Bit Testing in PHP ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a need (in my PHP script) to test whether a specified bit in a
32-bit word is set to 0 or 1.

I've googled around and have found stuff regarding the bitwise
operators in PHP but I haven't been able to come up with exactly what
I'm looking for. Can someone tell me how to do this or possibly point
me to a web site that has an example that I can adapt?


Re: Bit Testing in PHP ?

Jack wrote:
Quoted text here. Click to load it

echo 'bit='.(($word>>$bitNumber)&1);

Re: Bit Testing in PHP ?


Quoted text here. Click to load it

Alexey -

Thanks! That does exactly what I needed.

I wish I understood how it works :)
I'm going to study up on that a bit.

Re: Bit Testing in PHP ?

Quoted text here. Click to load it

It works like this:

$word contains some bits.  Let's say 11010001101.

The >> operator shifts those bits to the right.  So, to make it easy
to check the fourth bit we'll get it into the right-most position by
shifting the whole thing right three bits.  This leaves you with:

11010001101 >> 3 = 00011010001

Since the number one has a 1 bit in the right-most position we just
AND the two numbers together.  The result of an AND will be 1 where
both numbers have a 1 and will be 0 otherwise.  So, AND'ing the two
together gives you this:


Since that's not zero the first bit (which was originally the fourth
bit) must have been set.

We can try the same example and look at the second bit instead:

11010001101 >> 1 = 01101000110

then AND it with 1:


which is zero.  So the second bit wasn't set.

Does that make any sense?

Re: Bit Testing in PHP ?

On Tue, 5 Feb 2008 06:05:40 -0800 (PST), ZeldorBlat

Quoted text here. Click to load it

Yes, it does. Thank you for the explanation (I would have NEVER
figured that out)

Re: Bit Testing in PHP ?


Quoted text here. Click to load it

Usually you define some constants in order to easily access these bits
and to keep the code more readable:

define('BIT_1',  1);
define('BIT_2',  2);
define('BIT_3',  4);
define('BIT_4',  8);
define('BIT_5', 16);

(of course you should choose some better names)

The rest is easy:

* set a bit (OR):        $value = $value | YOUR_BIT_CONSTANT
* unset a bit (AND NOT): $value = $value & ~YOUR_BIT_CONSTANT
* get a bit (AND):       $isSet = $value & YOUR_BIT_CONSTANT


Re: Bit Testing in PHP ?


Quoted text here. Click to load it

Micha - Thanks.

I haven't tried this yet but it looks like it should do the trick.

As I commented to Alexey, I don't understand how all this works yet.
And, to add to my confusion, it seems your way is quite different than
his. I have some learning to do.    :)

Thanks again.

Re: Bit Testing in PHP ?


Quoted text here. Click to load it

Binary logic isn't that complicated. You just have to remember how an
integer is represented in binary form and how the basic logic operations
AND, OR and NOT work with single bits. Often it helps to look at a so
called truth table:

These tables show what the result of such a logical operation will be,
dependent on the operands. The most important ones are the first three,
representing the three operations mentioned above.

Quoted text here. Click to load it

The difference is that he uses bit shifting as explained in the reply
from ZeldorBlat, while my code directly accesses the bits by their

Every bit position represents a power of 2, starting with 2^0 = 1 for
the first bit. The 6th bit for example is 2^(6-1) = 2^5 = 32. In general
the nth bit has the value 2^(n-1). If you know these simple values, you
can directly access all the bits without any shifting.

This has the advantage that it's possible to set/test multiple bits at
the same time with a single operation. But of course in other cases bit
shifting might be more appropriate or more efficient. For example a
single shift to the right is a very efficient way for a division by 2.

I hope you are not even more confused now than before. ;)
But once you've understood the basic principles, binary logic and bit
manipulations can be a really powerful tool.


Site Timeline