June 23, 2009
Bitwise Operations: Part 2
Starting
So in order to be able to work with bits, we first need to recognize we will be doing bit level operations in binary, but it must be known that we can use hex code to make our lives much easier.
Do recognize that the following are identical in value and just differentiate in notation:
BINARY HEXDECIMAL DECIMAL 0001 0x01 1 0010 0x02 2 0011 0x03 2 1010 0x0A 10 1100 0x0C 12 1111 0x0F 15
(While reading this it will be tempting to ask, why is this important, or how can this be useful, and I assure you these topics will be covered in the next article.)
Links to Useful Reading
If you are not entirely comfortable with hex or binary yet, take a moment to do a bit more reading on either of these two topics;
Wikipedia on Binary:http://en.wikipedia.org/wiki/Binary_numeral_system
Wikipedia on Hexdecimal: http://en.wikipedia.org/wiki/Hexadecimal
Bitwise NOT
The Bitwise NOT inverses the binary digits, any bits that are off are turned on, and any bits that are on are turned off.
The Bitwise NOT operator (in PHP) is the Tilde(~).
~1101 (yields) 0010 ~0000 (yields) 1111 ~1010 (yields) 0101
Bitwise AND
The Bitwise AND operator compares two binary series and creates a resulting series. The resulting series will only have ON(1) bits where both values in that position were on.
0101 AND 1001 ------------- END 0001
As you can see above the end result is a series that has on bits only where bits in the same position were on, in this case the last position (left to right).
heres a couple more examples;
0101 1111 AND 1101 AND 1010 ------------- ------------ END 0101 END 1010
Bitwise OR(inclusive OR)
The Bitwise OR operator compares two binary series and creates a resulting series. The resulting series will have ON(1) bits where either value or both values in that position were on.
0101 OR 1001 ------------- END 1101
From the example above you can see that the “inclusive or” looks for any bits that are on and places an on bit in that position for the end result. Heres a couple more exmples.
0101 1111 OR 1101 OR 1010 ------------- ------------ END 1101 END 1111
Bitwise xOR(exclusive OR)
The Bitwise OR operator compares two binary series and creates a resulting series. The resulting series will have ON(1) bits where either value, but NOT both values in that position is on.
0101 xOR 1001 ------------- END 1100
From the example above you can see that the “exclusive or” looks for any bits that are on and places an on bit in that position for the end result, aslong as both bits are not ON in that same position. Heres a couple more exmples.
0101 1111 xOR 1101 xOR 1010 ------------- ------------ END 1000 END 0101
Bitwise SHIFT
Using two greater than or less than signs concurrently, we can shift the digits in the binary number to the left or righ by a specified amount.
LEFT SHIFT RIGHT SHIFT ------------------ ------------------ 0011 << 2 1100 >> 2 END 1100 END 0011
What happens when a shift occurs is all the digits are litteraly shifted to a direction, and zeroes are put in as fillers to the left or the right.
Signs
We have not begun to use the Bitwise operators in our examples in order to try and convey the meaning behind them a bit better.
Bitwise Operations use their own set of operators which include the following, and it should be noted that these operators are very common, but you should look up bitwise operators for your language of choice.
& BITWISE AND | BITWISE OR ^ BITWISE xOR ~ BITWISE NOT >> BITWISE SHIFT RIGHT << BITWISE SHIFT LEFT
*Note, these operators night be different depending on language specifications, the ones shown here are primarily taken from PHP, as that we be the language used to provide an example in later articles.
NextKeep in mind what these operators do, next Article we will go over how their interaction affects the result, the purpose of this was to introduce you into how these operators work on the binary numbers.