473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Bit Testing in PHP ?

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?

Thanks.
Feb 4 '08 #1
5 5516
Jack wrote:
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?

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

Feb 4 '08 #2
On Mon, 04 Feb 2008 22:04:04 +0000, Alexey Kulentsov <ak**@inbox.ru>
wrote:
>Jack wrote:
>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?

Thanks.

echo 'bit='.(($word>>$bitNumber)&1);
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.
Feb 4 '08 #3
On Feb 4, 6:56 pm, Jack <ironwoodcan...@gmail.comwrote:
On Mon, 04 Feb 2008 22:04:04 +0000, Alexey Kulentsov <a...@inbox.ru>
wrote:
Jack wrote:
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?
Thanks.
echo 'bit='.(($word>>$bitNumber)&1);

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.
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:

00011010001
00000000001
-----------
00000000001

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:

01101000110
00000000001
-----------
00000000000

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

Does that make any sense?
Feb 5 '08 #4
On Tue, 5 Feb 2008 06:05:40 -0800 (PST), ZeldorBlat
<ze********@gmail.comwrote:
>On Feb 4, 6:56 pm, Jack <ironwoodcan...@gmail.comwrote:
>On Mon, 04 Feb 2008 22:04:04 +0000, Alexey Kulentsov <a...@inbox.ru>
wrote:
>Jack wrote:
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?
>Thanks.
>echo 'bit='.(($word>>$bitNumber)&1);

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.

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:

00011010001
00000000001
-----------
00000000001

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:

01101000110
00000000001
-----------
00000000000

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

Does that make any sense?
Yes, it does. Thank you for the explanation (I would have NEVER
figured that out)
Feb 5 '08 #5
..oO(Jack)
>On Mon, 04 Feb 2008 23:51:28 +0100, Michael Fesser <ne*****@gmx.de>
wrote:
>>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

I haven't tried this yet but it looks like it should do the trick.
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:

http://en.wikipedia.org/wiki/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.
>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. :)
The difference is that he uses bit shifting as explained in the reply
from ZeldorBlat, while my code directly accesses the bits by their
values.

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.

Micha
Feb 5 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
0
by: Brian Russell | last post by:
We have three servers (beyond my development box) in our organization. The first is a testing server that has IIS and SQL Server on it. The second is another testing server that also has IIS and...
4
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
58
by: nw | last post by:
Hi, I have been asked to teach a short course on testing in C++. Until now I have used my own testing classes (which from what I've seen seem similar to the boost unit testing classes)....
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
0
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.