LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2005, 02:49 AM   #1
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Rep: Reputation: 15
"Bit" Of Help Please!=


Hi,

I am a bit confused here. I have an example of bit operators verse logical operators from http://cplus.about.com/od/advancedtu.../aa042203f.htm . I was wondering if I am on the track saying that a bit operator does the same thing as a logical operator with the only exception that it does it on bits rather then variables ect?

#include <iostream>
using namespace std;

int main()
{
unsigned short flags1;
unsigned short flags2;

flags1 = 0x00FF;
flags2 = 0xFF00;

cout << "Bitwise Operator: " << (flags1 & flags2) << endl;
cout << "Logical Operator: " <<(flags1 && flags2) << endl;

return 0;
}

Thanks for your helpl
 
Old 07-17-2005, 04:45 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
AFAIK, with && both will be converted to bool first for integral/POD types, then the result returned will also be bool. For &, each bit is treated as its own bool and like positions are compared:
Code:
flags1 && flags2 -> true && true (anything non-zero is true) -> true

flags1 &  flags2 -> 0000 0000 1111 1111 &
                    1111 1111 0000 0000   ->
                    0000 0000 0000 0000
If either operand is a class object then the operation will only be evaluated if there is an overloaded/global operator that will take both operands as arguments in the order that they are given. In that case, it's up to the implementor to decide how the operator will behave.
ta0kira
 
Old 07-19-2005, 04:05 PM   #3
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
just as a side note: If you want to combine your bitfields then you can 'or' them:

0000 0000 1111 1111 & 1010 1010 0000 0000 = 1010 1010 1111 1111
 
Old 07-19-2005, 04:14 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You mean:

0000 0000 1111 1111 | 1010 1010 0000 0000 = 1010 1010 1111 1111
 
Old 07-20-2005, 12:48 AM   #5
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
Narg.. Yes. I meant | instead of &. Sorry bout that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 04:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Error for wireless request "Set Bit Rate" (8B20) king_scott_2 Linux - Wireless Networking 3 06-26-2006 05:27 PM
What do you recommend ""I want to built my Pc using AMD 64 bit."" nuketemala Linux - Hardware 1 05-01-2004 01:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration