Bitwise Operators
// March 19th, 2009 // HPDC
Bitwise operators let you manipulate individual bits. Normally I would not bother writing about this, but I believe knowing about bit wise operation is an important asset to have when trying to optimize code. Especially in memory consumption. Keep in mind that all bitwise operations in C only work of singed and unsigned integer primitive data types, mainly:
- char
- short
- int
- long
Article outline
I’ll talk about each of the following operations and give some examples on how to use each.
- Bitwise NOT (~)
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Shift Left (<<)
- Shift Right(>>)
Bitwise NOT (~)
This is an unary operator. It simply filps all ‘0’s to ‘1’s and ‘1’s to ‘0’s. Here is a simple example:
unsigned byte x = 10; x = 00001010 ~x = 11110101
Bitwise AND (&)
This is a binary operator. Just follow the truth table. Both bits have to be true to get a resulting true bit. Anything else is false. Here is a simple example:
unsigned byte x = (10&8); 10 = 00001010 8 = 00001000 x = 00001000
Bitwise OR (|)
This is a binary operator. Just follow the truth table. Here, just one of the bits has to be true to get a resulting true. Here is a simple example:
unsigned byte x = (10|8); 10 = 00001010 8 = 00001000 x = 00001010
Bitwise XOR (^)
This is a binary operator. Just follow the truth table. Basically it flags all bits that are different. Here is a simple example:
unsigned byte x = (10^15); 10 = 00001010 15 = 00001111 x = 00000101
Shift Left (<<)
Keep in mind that when you do one of the shift operators the size of the data type does not change.
A left shift, moves the bits to the left inserting ‘0’s in the right most bit for every shift. The left most bit is discarded. This is effectively multiplying the number by 2. Note if you use too many shifts you could end up with a negative number. This is possible if the data type is interpreted as 2’s complement. Here is a simple example:
unsigned byte x = 4 << 2; 4 = 00000100 x = 00010000 x = 16
Shift Right (>>)
A rightshift, moves the bits to the right inserting ‘0’s in the left most bit for every shift. The right most bit is discarded. This is effectively dividing the number by 2. Note if you use too many shifts you could end up with a zero. Here is a simple example:
unsigned byte x = 4 >> 2; 4 = 00000100 x = 00000001 x = 1
That wraps up the bitwise operators. If there is anything I forgot, let me know and I’ll make the updates. I hope you learned something in this read. Remember these are operation which the processor can execute very fast. For example, if you every need to multiple or divide by 2, it best to use the shift operations. Not only will you code execute faster, but also save you processor some energy!
Leave a Reply
- buy cipro without a prescription said: In truth, immediately i didn't understand the esse...
- lazy said: thanks nice site http://csharptalk.com ...
- mez said: I have decided to hold off on this, cause the lang...
- Adam said: Taxes are easy for you and me, but when you start...
- az said: lol- I remember those days. Just remember to keep...
- Wei said: Hey.This is Cool ^0^...



