1. ## Bitwise operators

I've seen what they are supposed to do, and some explanation as to what they can be used for, but I'm still lost.

What do they do and how do you know when to use them. When you do use them what are the results? For what purpose in normal coding as well as neat tricks are they used? I've read books and the basic books just tell you what they are, then the advanced books assume you know them in and out, nobody seems to be able to explain this so that I can understand.

Thanks a bunch

2. ## a similar thread exists

seh_hui asked the same question on the thread "What's the use of bitwise operators?" on December 4th 2001.

klausi

3. I read that, it gave a bunch of vague uses and a couple of good tricks, but nobody explained what they do or why
For example,
if(x & 1)
printf("odd");

Why does this work, how does it work? I'm looking for someone to explain to me why they are used, how they are used, and what they actually do without using the vague explanation that all books give. The second two questions are far more important to me than the first at the moment though.

4. i dont know fully what bitwise operators are available - i know OR and XOR are , also AND . thaye combine values, search google for their truth tables.

5. | - OR
& - AND
^ - XOR
~ - Unary (opposite)
<< - shift left
>> - shift right

OR is fairly simple:
Code:
```(1 | 2)

00000001
|  00000010
-----------------
00000011```
It basically just adds the binary of the numbers. It doesn't actually add the numbers though, remember that.

AND is just as easy:
Code:
```(1 & 3)

00000001
&  00000011
------------------
00000001```
See what it did? everywhere there was a 1 in the first one and the second, there was a 1 in the resulting number.

XOR is trickier:

Code:
```(1 ^ 3)

00000001
^  00000011
------------------
00000010```

Anywhere there was a 1 in both numbers, it became a zero.

UNARY is the easiest. It basically means binary opposite:

Code:
```(~1)

~0000000001 = 1111111110```
<< and >> (shift left and right) are incredibly simple, and used the most:

000000010 >> 1 = 000000001
000000001 << 1 = 000000010

It basicall goes like this: number >> Amount to shift by
It moves the numbers down by the number you place to the right.

>> Is actually the quickest way to divide by 2
<< Is actually the quickest way to multiply by 2

Another thing about shifts, is that the numbers don't carry! soo:

000000001 >> 1 = 00000000, NOT 10000000

And remember I wrote the numbers out in binary instead of the actual numbers you'd put in there. ( 000000011 is 3, 0000000010 is 2, and 000000001 is 1 binary) So remember, you don't write the numbers in binary, you write them in decimal to use the bit operators.

6. To fully understand how bitwise operators work, you should have an understanding of bits in genereal. Read this binary primer (or try here). After gaining an understanding of binary digits from one of the links, you should be ready to understand some basic bit math.

Binary AND (&)
The binary AND (&) compares two bits and returns true (1) if both bits are set to true.
• 0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1

Binary OR (|)
The binary OR (|) compares two bits and returns true if either or both bits are set to true.
• 0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1

Binary Exclusive OR (XOR) (^)
The binary exclusive OR (^), also known as XOR, compares two bits and returns true if only one of the bits are set to true.
• 0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0

In C, you are normally applying binary operators against a group of bits organized as a byte or multiple bytes. For example, if we took the numbers 5 and 3 and XOR'd them together, it would look like this:
• 00000101 (5)
00000011 (3)
-------------
00000110 (6)

Using bitwise AND to see if a number is odd is done with:
Code:
```if ( a & 1 )
printf("Odd");```
Assume the variable 'a' equals thirteen:
• 00001101 (13)
00000001 ( 1)
-------------
00000001 ( 1)

For every odd number, the lowest order bit will be set true. For the number 1 (00000001), the only bit set is the low order bit. If we AND them together, we can see if the number in question has the low order bit set, and if it does, the number is odd. This works because the expression "a & 1" will return 1 (TRUE) if the low order bit is set and 0 (FALSE) if the bit is not set (clear).

We can also see if a character is uppercase or lowercase because, in ASCII, the only difference between an upper or lower case letter is whether the bit that represents 32 is true (it is true for lowercase letters). Let's take the letters 'c' and 'C'. If we AND either with 32, we can see if they are upper or lower case:
• 01000011 (C == 67)
00100000 (32)
-------------
00000000 (0 == FALSE)

01100011 (c == 99 )
00100000 (32)
-------------
00100000 (32 == TRUE)

So, the expression "some_char & 32" is TRUE if the variable 'some_char' holds a lowercase letter. Does this help at all?

7. Oops, -KEN- was too fast for me

8. Oh yea, that helps a lot. Thanks KEN and Deckard
I'm going to read that page you gave me and then try to understand better, is it okay if I ask you more questions as I figure out how to word them?

9. Originally posted by Unregistered
is it okay if I ask you more questions as I figure out how to word them?
Absolutely. That's why we keep coming back :)

10. ## Bitwise operators

very very cool. I was having problems with the bitwise operators as well.

I was wondering how you would do a XAND on a bit?

would

a = ~a
a = &a

work? and am i thinking of bitwise operators properly now?

Thanks again

11. What exactly would an exclusive AND do?

12. ## Bitwise operators

I was thinking that an XAND could be used in something like a netmask for internet applications the got point to point.. or used to catch broadcast messages from a subnet like a DHCP or bootp signal.

13. That's not really what I meant...

An XOR is true when A or B is true, exclusively (when onely one of them is on). So, how would XAND work? If it's true exclusively when A and B are true, then it's an AND - it's it's true when neither are true, then it's a NAND? When would an XAND be true?

14. An AND is an exclusive operator since it's only true when both A and B are true. So there is no such as a XAND.

15. > So there is no such as a XAND.

That's kinda what I was trying to get Unreg. to understand.