LinuxQuestions.org
Visit Jeremy's Blog.
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 04-21-2004, 07:56 PM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
Bitwise operators


Hi Guys!
I was checking out bitwise operators, and I was wondering, how is a condition determined with bitwise operators. e.g.

Code:
if(status & PAPER_OUT)
  { /* do something */ }
Suppose status is an int containing the current status of the printer. No, obviously, when they are &ed together, some bits will be 0, some will be 1. How will the compiler determine, whether it's true or false? PAPER_OUT is I think self-explanatory. One more thing, how do you send output to ports in Linux and also in Windows. Can anybody please help me?
 
Old 04-21-2004, 08:25 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
You are probably talking c code here.

'if(status & PAPER_OUT)' is first evaluated as:
'if(aValue)', where aValue is the resulting integer having ones and zeros according to the bitwise-and operation.
Next this value is interpreted as a truth value with 0=false, others=true.

In other words: 'if(status & PAPER_OUT)' is equivalent to 'if( (status&PAPER_OUT) != 0)'
 
Old 04-21-2004, 11:18 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Usually, something like your 'status' variable is used to hold flags (such as PAPER_OUT). What you do is you assign each flag a unique bit. Something like this:

Code:
#define VAL1 0x1   // 00000001
#define VAL2 0x2   // 00000010
#define VAL3 0x4   // 00000100
#define VAL4 0x8   // 00001000
#define VAL5 0x10 // 00010000
#define VAL6 0x20 // 00100000
#define VAL7 0x40 // 01000000
#define VAL8 0x80 // 10000000
That way you can set flags in the variable. Something like status = VAL4 | VAL5 would set the status to 00011000. So you can probably see where this is going. Each flag has to only set 1 bit so values like 3, 5, 7, etc. wouldn't work since they look like 00000011, 00000101, 00000111, etc.

So to set a flag you use the | operator.
To query a flag you use the & operator.
To clear a flag you'd use & coupled with ^. Something like status &= ^VAL2 (status = 01100110 & 11111101 which would give you all the bits that were set before except the 2nd).

C interprets anything that's not 0 as true, like ToniT said. Not sure if that's enough detail. Let me know if you need more
 
Old 04-21-2004, 11:36 PM   #4
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
take a look at one microprocessors book at the "masquerading" section.
 
Old 04-21-2004, 11:42 PM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
and for ports.... I think this will help:
http://www.si.hhs.nl/~bertn/gnu_libc...74.html#SEC174
 
Old 04-22-2004, 02:34 AM   #6
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
I get how the bitwise operators work. But actually, what I wanted to ask was, in a condition like the one I posted, how does the computer determine whether the condition is true. I mean we're not comparing the result to any other value. Then how can the computer tell whether the resulting number, consisting of both zeros and ones, is true or false. I know 0 is evaluated as false and any other number is evaluated as true. But I'm having trouble figuring out how a combination of both can be evaluated without comparing it to any other value.
 
Old 04-22-2004, 03:53 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Don't think of it as a combination of both...an int is 32-bits. The 32 1's and 0's make up the single integer. So basically, if any single bit is 1 then it's determined to be true (since it makes the int non-zero).
 
Old 04-22-2004, 10:50 AM   #8
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
you have already been answered that.

if will evaluate true when the argument provided is different to 0. Said it another way: 0 is false... anything else is true.
 
Old 04-22-2004, 10:46 PM   #9
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Ok thanks guys. My question was actually something else. But I figured it out myself so no sweat. This thread helped a lot too. Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 01:12 PM
Operators *^ and >> GodSendDeath Programming 4 11-01-2004 09:47 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
operators linuxanswer Programming 3 12-14-2003 06:09 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 07:36 AM


All times are GMT -5. The time now is 11:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration