Bitwise Operators Tutorial

This tutorial is only going to be short - it's a pretty simple/easy to understand concept. While I've used PHP as the language in the thread title, the code and concept is quite universal to all languages.

Base 10 number system

We use a number system of base 10. Why? Because humans have 10 figures and 10 toes. If we had only four fingers on each hand we would no doubt use a base 8 number system. The significance? None, really.

Let's take a simple number: 1,234. We can break that number up in to components of 10.

See how that works?

Base 8 (octal) number system

In PHP, if I were to go:

You probably exepct it to output 21. However, it outputs 17. Why is it so? Simple; you aren't adding two numbers with base 10, instead you're adding two numbers with base 8. Let's perform the exact same exercise as we previously did, but instead of using powers of 10, let's use powers of 8.PHP Code:

`print (int) 010 + 011;`

When we convert them both from base 8 to base 10 and add them, we get 17. 8 + 9 =17.

Base 2 number system (bits and bytes)

With a base 10 number system, we can only use digits 0-9. With base 8, we can only use 0-7. It follows logically that, with base 2, we can only use 0-1.

Bits are very important in terms of computing; I won't go too much in to it, as that isn't really the purpose of this tutorial. However, generally a bit can be thought of as having an off (0) and on (1) state (or vice versa).

A bit is a single digit between 0-1, or a single base 2 digit.

A byte is a collection of 8 bits. Therefore;

1 is an example of a bit.

10110101 is an example of a byte.

Let's take the latter example and, again performing the same exercise, let's convert it from a base 2 number to a base 10 number. Going from right to left;

Therefore, the byte 10110101 is a representation of the base 10 number 181. Simple?

Bitwise Operators

Now we know what bits and bytes are, let's start playing with them, using bitwise operators. Let's first cover the first three.

And, or (inclusive), xor (exclusive)

And operator

The above code outputs 7. Let's took a moment to understand why. First, let's convert them both digits to their respective bytes. To do this, we break them up in to powers of 2.PHP Code:

`print 15 & 7;`

15 = 8 + 4 + 2 + 1 = 1*2^0 + 1*2^1 + 1*2^2 + 1*2^3

Which of course, when we use 8 bits to create a byte comes out as 00001111.

7 = 4 + 2 + 1

Which comes out to 00000111

The & operator compares bits that are set in both A & B (let's take the left byte as A and the right as B). So, with the above example, we can see that both A & B share the three 1's to the far right (they also share the four leftmost 0s, however as they will always come out as 0, we can ignore that). So, they share the three rightmost digits or:Code:00001111 00000111

00000111 which comes out to 7 (1 * 2^0 + 1*2^1 + 1*2^2)

Bitwise Or operator

The bitwise OR operator checks for bits that are set in either A or B.PHP Code:

`print 15 | 7; // Outputs 15`

So for example, 15 has the four rightmost bits set to one, which comes out to: 8 + 4 + 2 + 1.

7 has the three rightmost bits set to one, coming out to: 4 + 2 + 1.

Now we group the digits that are set in either A or B, whick are 8,4,2,1, which when we add come out as 15. Huzzah?

Exclusive or (XOR) Operator

The exclusive or (^) operator checks for bits that are set in either A or B, but not both. Again, using the last two bytes, we can see that the only bit set in either A or B, but not both is the 4th from the right. 1 * 2^4, which is 8. Therefore:

Again, quite simple.PHP Code:

`print 15 ^ 7; //Outputs 8`

Not

Imagine you want to search for bits that are in A and not in B, or in B and not in A. We can now use the not operator, which is represented by a tilda (~).

In this specific example, we can see that the only bit set in A and not B is again fourth from the right, which is 8.PHP Code:

`print 15 & ~7; //Outputs 8`

Left shift, right shit

Shifting left

Let's start shifting bits within bytes! Let's take a basic byte for the digit 11; 00001011. Let's say we wanted to shift all of those bits 2 spaces to the left.

i.e. turn the top to the bottom;

[code]00001011

00101100[/php]

See how we shifted the bits 2 to the left? We could achieve that by:

It outputs 44, which corresponds to the byte 00101100; Note that it's also the same as multiplying 11 by 2^2. You should see a pattern:PHP Code:

`print 11 << 2; // Outputs 44`

11 << 2 is the same as 11 * 2^2

11 << 5 is the same as 11 * 2^5

Shifting right

If << shifts left, then surely >> shifts right. Right? Right.

The bottom byte is the top byte whos bits have been shifted two to the right. It's the same as:Code:00110100 00001101

The same as 52 / 2^2. The pattern is obvious.PHP Code:

`print 52 >> 2; // Outputs 13.`

And that sums up bitwise operators in PHP. If I missed anything, let me know.

Results 1 to 4 of 4

# Thread: [PHP] Bitwise Operators Tutorial

- 10 Jan. 2011 10:29am #1
## [PHP] Bitwise Operators Tutorial

Last edited by Artificial; 10 Jan. 2011 at 11:36am.

- 14 Jan. 2011 01:26am #2

- 14 Jan. 2011 07:08pm #3
Lol, what riddle said.

Nice tut though. Bitwise becomes very useful once you get into using hardware.

Originally Posted by**Artificial**Originally Posted by**Artificial**

- 14 Jan. 2011 08:20pm #4