Solved

# How to check a specific bit field using PHP?

Posted on 2009-04-30
227 Views
Hello everybody!

I am not great at using bit fields and wondering if something is possible.

I created a bit field that is something like  01011.  Each one or zero represents whether a certain option should be enabled in my application.  Is there a way to check if for instance the second digit is a one or zero?

What is the best way to check each digit individually and see if it is a 1 or 0?  Thanks!
0
Question by:parlays

LVL 4

Expert Comment

If you provide a small code snippet of your problem I can probably give a solution. However, the issue is a matter of using bitwise operations.

http://en.wikipedia.org/wiki/Bitwise_operation

Essientially if you had 01011 and you wanted to check for the value of the second bit from the left you would do a bitwise "AND" with 01000 which would return 01000. If the you had 00011 it would return 00000.
0

Author Comment

Nice, thank you, but it's hard in PHP because whenever I try to echo out the result, if a number starts with zero it converts it from octal?  How can I keep it as a bitfield?
0

LVL 4

Expert Comment

What are you trying to echo to? Are you just trying to display the result or will something need to use that result once echoed?

If all you are trying to do is display the result then just wrap it in quotes and it will display it as characters.
0

Author Comment

Well I put this:

\$a = 0110
\$b = 1101

\$result = \$a & \$b;

echo \$result;

There is a strange conversion going on in the process, \$a and \$b are being treated as integers and converted to bit values in the process.  How do I specify from the start they are bit values?
0

LVL 11

Accepted Solution

\$a1 = bindec('0110');
\$b1 = bindec('1101');

echo \$a1 & \$b1; // shows 4
echo decbin(\$a1 & \$b1); // shows 100

- - -
\$a = 0110
Leading zero assigns octal (base 8) value (72 base 10)
1001000 base 2

\$b = 1101
Assigns decimal (base 10) value
10001001101 base 2

10001001101 &
00001001000
-----------------
00001001000

0

LVL 11

Assisted Solution

0

LVL 4

Assisted Solution

Spoxox beat me to it. I was just about to post something just like that.

One thing to note however, is that when you echo out the results you will be missing the leading zeros. One easy fix for that would be to check the length of the string and determine how many characters short it is from what it should be if you had the full number. Then simply add that many zeros to the beginning of the string.
0

LVL 11

Assisted Solution

\$format = "%04b & %04b = _%04b_\n";
printf(\$format, \$a1, \$b1, \$a1 & \$b1 );

prints
0110 & 1101 = _0100_

http://ca3.php.net/manual/en/function.sprintf.php
0

LVL 11

Expert Comment

Aha, clearly eggs and I think alike. I shall henceforth be known as bacon.
0

LVL 4

Expert Comment

Hehe. Yeah, moments after I posted that last comment I was just about to post info on printf formatting.
0

LVL 19

Expert Comment

I had to see how a simple question could garner 10 responses. now I know.

:)
0

Author Closing Comment

Thank you guys so much!!
0

## Featured Post

### Suggested Solutions

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…