• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

How to check a specific bit field using PHP?

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
parlays
Asked:
parlays
  • 4
  • 4
  • 3
  • +1
4 Solutions
 
my2eggsCommented:
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
 
parlaysAuthor Commented:
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
 
my2eggsCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
parlaysAuthor Commented:
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
 
spoxoxCommented:

$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
 
my2eggsCommented:
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
 
spoxoxCommented:

$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
 
spoxoxCommented:
Aha, clearly eggs and I think alike. I shall henceforth be known as bacon.
0
 
my2eggsCommented:
Hehe. Yeah, moments after I posted that last comment I was just about to post info on printf formatting.
0
 
Michael701Commented:
I had to see how a simple question could garner 10 responses. now I know.

:)
0
 
parlaysAuthor Commented:
Thank you guys so much!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now