I have a database with a field containing a bit mask, and I have a hex bit mask list which looks like this:
2^8 256 = Type 1
2^9 512 = Type 2
2^0 001 = Type 3
2^4 016 = Type 4
2^1 002 = Type 5
2^5 032 = Type 6
I'm trying to "decode" (not sure of the right term)
with php what the bit mask was, for example: 003
How would I know that 003 was a combination of 002 & 001?
Please, I dont know anything about this, help shed some light.
I need to be able to do it with php.
First, you should define constants for your values:
const TYPE1 = 0b01000000;
const TYPE2 = 0b10000000;
const TYPE3 = 0b00000001;
const TYPE4 = 0b00010000;
const TYPE5 = 0b00000010;
const TYPE6 = 0b00100000;
This will keep you sane. I used binary (0b notation), but you could use hex (0x) or decimal just as easily; I think binary makes it easiest to see which bit represents which constant though, and when you're defining new constants you can easily see which bits have already been used. Bear in mind that constants' names are case-sensitive... but just stick with ALL_CAPS constants and you'll save yourself a lot of headache down the road.
Then you can use the logical operator & to test for each one, as per the comment by #MarkBaker :
if ($mask & TYPE1) echo "Type 1 is set\n";
if ($mask & TYPE2) echo "Type 2 is set\n";
if ($mask & TYPE3) echo "Type 3 is set\n";
if ($mask & TYPE4) echo "Type 4 is set\n";
if ($mask & TYPE5) echo "Type 5 is set\n";
if ($mask & TYPE6) echo "Type 6 is set\n";
For your example of the value 3, you'd get an output of:
Type 3 is set
Type 5 is set
Also, a caveat when working in PHP (and many other languages) -- numbers written with a leading zero, e.g. 032 in your question, are interpreted as octal! Therefore 032 is not actually 32, but rather 26!
Related
Is there any database similiar to bit (byte) in laravel, could not find any in the documentation. (https://laravel.com/docs/5.1/migrations).
I try to do something like:
00000001 -> stands for something lets say playstation
00000010 -> stands for xbox
00000011 -> stands for both above
Instead of trying to use the BIT datatype, which would be a bit of a hassle to work with, you can just use an integer and bitwise operators instead, assuming you don't need more than 32 options for one field (bigint = 8 bytes = 32 bits).
As a very simple example, imagine you create the following enum class:
class Bitmask {
const ATARI = 1 << 0; // 00000001 (1)
const NES = 1 << 1; // 00000010 (2)
const SNES = 1 << 2; // 00000100 (4)
const SEGA = 1 << 3; // 00001000 (8)
const PLAYSTATION = 1 << 4; // 00010000 (16)
const XBOX = 1 << 5; // 00100000 (32)
}
To set the field, all you need to do is add the bitmasks together (in this configuration, ORing (|) them is the same). If a user has an NES and a PLAYSTATION:
$user->systems = Bitmask::NES + Bitmask::PLAYSTATION;
To query, you would use the bitwise operators. So, if you wanted the users that have SEGAs:
User::where('systems', '&', Bitmask::SEGA)->get();
If you wanted the users that have PLAYSTATIONs or XBOXes:
User::where('systems', '&', Bitmask::PLAYSTATION | Bitmask::XBOX)->get();
The & operator will perform a bitwise AND operation between the integer field and the integer you pass in. If any of the bits match up, the & operator will return a value > 0, and the where clause will be truthy. If none of the bits match up, the & operator will return 0 and the where clause will be false.
Sometimes in programming they allow one to chain parameters in a single function input variable like the second input variable below:
define('FLAGA',40);
define('FLAGB',10);
define('FLAGC',3);
function foo($sFile, $vFlags) {
// do something
}
foo('test.txt',FLAGA | FLAGB | FLAGC);
PHP calls this single pipe character (|) the Bitwise OR operator. How do I now add something inside foo() to test $vFlags to see which flags were set?
I think you'll find that flags like this are normally defined as powers of 2, e.g.:
define('FLAGA',1);
define('FLAGB',2);
define('FLAGC',4); /* then 8, 16, 32, etc... */
As you rightly stated, these can be combined by using a bitwise OR operator:
foo('test.txt',FLAGA | FLAGB | FLAGC);
To test these flags inside your function, you need to use a bitwise AND operator as follows:
function foo($sFile, $vFlags) {
if ($vFlags & FLAGA) {
// FLAGA was set
}
if ($vFlags & FLAGB) {
// FLAGB was set
}
//// etc...
}
The "flags" parameter would be called a bitmask. A single byte contains 8 bits which are either set or not set. You simply assign your own meaning to every bit; if it's set it means yes for that particular bit, otherwise no.
So you need to start by defining your flags with the correct values which set the right bits; just arbitrary numbers won't combine together in the right ways:
define('FLAGA', 1); // 00000001
define('FLAGB', 2); // 00000010
define('FLAGC', 4); // 00000100
define('FLAGD', 8); // 00001000
Given the above, FLAGB | FLAGD creates a bit mask with the second and forth bit set (00001010). You need to get somewhat comfortable with converting between base 2 (binary) and base 10 (decimal) for this.
To test for this, you use &:
$flags = FLAGB | FLAGD;
if ($flags & FLAGA) {
echo 'flag A is set';
}
if ($flags & FLAGB) {
echo 'flag B is set';
}
..
You need the bitwise AND operator &:
define('FLAGA',40);
define('FLAGB',10);
define('FLAGC',3);
function foo($sFile, $vFlags) {
if ($vFlags & FLAGA) {
echo "FLAGA is set\n";
}
if ($vFlags & FLAGB) {
echo "FLAGB is set\n";
}
if ($vFlags & FLAGC) {
echo "FLAGC is set\n";
}
}
foo('test.txt',FLAGA | FLAGB | FLAGC);
DEMO
That said, bitwise operations necessarily work in terms of binary, where each bit represents a power of 2. So, you are typically going to want to define flags in powers of 2, as in
define('FLAGA',1);
define('FLAGB',2);
define('FLAGC',4);
define('FLAGD',8); // etc.
Otherwise, imagine this scenario:
define('FLAGA',8);
define('FLAGB',32);
define('FLAGC',40);
If you have a value 40 for $vFlags, you can't tell what flags are set; it could be any of the following:
FLAGA & FLAGB
FLAGA & FLAGB & FLAGC
FLAGA & FLAGC
FLAGB & FLAGC
FLAGC
Can somebody explain how this works? Thanks a lot :)
define("FLAG_A", 1);
define("FLAG_B", 2);
define("FLAG_C", 4);
function test_flags($flags=false) {
if ($flags & FLAG_A) echo "A";
if ($flags & FLAG_B) echo "B";
if ($flags & FLAG_C) echo "C";
}
test_flags(FLAG_A|FLAG_B);
This is done using what is called bitwise math. Specifically, they are using the & (bitwise and) operation to check to see if a value is set. For the sake of argument here, we will use 4-bit numbers, to keep it short and simple.
The number 7, in 4-bit binary, is as follows:
0111
Each digit has a value that is doubled each time, and all of them are added together to make the total number of 7. Broken down, it works like so:
0 1 1 1
^ ^ ^ ^
| | | |
8 4 2 1
So, essentially, it is:
0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 7
Now, with bitwise math, specifically the bitwise and, we can say that we only care about bits in certain columns -- basically, the bit in each column must be 1, or it will be set to zero. So, checking for '4' inside '7' with bitwise math:
0111 (7)
& 0100 (4)
______
0100 (4)
Since this value is non-zero, it is true, and we can determine that the value 4 is inside the value 7. Now consider the number 11:
1 0 1 1
^ ^ ^ ^
| | | |
8 4 2 1
1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 11
Try looking for 4 in that
1011 (11)
& 0100 (4)
______
0000 (0)
Since it has a value of 0, it is false, and we can consider that the number (4) is not inside the number 11.
Similarly, we can conclude that numbers 4, 2, and 1, but not 8, are in 7. In 11, we have 8, 2, and 1. By treating numbers as a series of bits, instead of a singular value, we can store many flags inside a single integer.
Definitely do some more reading on bitwise math, it can be very useful when used correctly, just don't try to shoe-horn it into everything.
This is based on bitwise operators. Think of
define("FLAG_A", 0001); // in binary form
define("FLAG_B", 0010);
define("FLAG_C", 0100);
Hence, e.g., $flags & FLAG_A is a bitwise AND between the variable $flags and the constant FLAG_A.
Sorry for the title. I wasn't sure how to ask this question.
I have a form on a website that asks a question. The answers are in check box form. Each answer is saved into my database with a 'score', the values look like this:
Allergy 1
Cardiology 2
Chest Disease 4
Dermatology 8
Emergency Room 16
Ambulance Trips 32
Gastroenterology 64
General Medicine 128
Gynecology 256
Hematology 512
Neurology 1024
Obstetrics 2048
Opthamology 4096
Orthopedics 8192
Physical Therapy 16384
Plastic Surgery 32768
Podiatry 65536
Proctology 131072
Psychiatry 262144
Surgery Performed 524288
Thoracic Surgery 1048576
Urology 2097152
Outside X-Rays 4194304
Diagnostic Tests (outside) 8388608
As you can see, the score is the previous value times two. When a user fills out the form, the answer is saved in the database as one value - all the answers added together.
For example, a user selected the values: Allergy, General Medicine, Hematology, Obstetrics. In the database, the answer for this question is saved as 2689.
Is there a way to figure out what answers have been selected by only having the answer to the question?
For example, I would query my database and pull the 2689 value, and I need to determine what answers were checked.
edit: I was hoping to reverse engineer the answer in PHP.
Yes, this is a common pattern called bit masking. Use your language's binary AND operator on the value corresponding to a given answer and the value submitted from the form to see if the given answer was one of the selected choices. For example, if the answer submitted and saved is 2689 as in your example, you can check whether "chest disease" was one of the selected choices by seeing if 2689 & 4 is nonzero. (& should be substituted with whatever the binary AND operator is in your language of choice.)
Note that this only works as long as all the values corresponding to individual choices are powers of 2. In general, the question posed in your title, about finding out what numbers from a given set have been added to come up with a given sum, is an instance of something called the knapsack problem and is only known to be solvable by checking every possible combination, which is very inefficient. (NP-complete, specifically)
You can find the values by ANDing with powers of 2.
20 = 1
21 = 2
22 = 4
23 = 8
...
223 = 8388608
You can find out the value of 2n using binary shifting like this: 1 << n
php like code:
$item[] = {"Allergy", "Cardiology", ..., "Diagnostic Tests (outside)"};
$answer = 2689;
for ( $power = 0; $power < count($item); $power++ ) {
if ( 1 << $power & $answer ) {
echo $item[$power] . "\n";
}
}
Edit: made it more php friendly
Yes, there is. Note that each k'th "score" is of the form 2^(k - 1), which corresponds to a bitstring with only the k'th bit set. If you know which bits are set, you can reconstruct the sum.
Taking 2689 as an example, we first need to write it out in binary:
2689 = 101010000001b
Counting from the right, we see that the first, eighth, tenth and twelfth bits are set, so (as you can verify)
2689 = 2^0 + 2^7 + 2^9 + 2^11
= 1 + 128 + 512 + 2048
The actual implementation of this can be done efficiently using bitwise operations. By taking the AND of the value and each of the "scores" in turn, then checking whether that gives a non-zero value, we can check which scores went into the sum.
this will do exactly what you wanted :
<?php
Print bindecValues("2689");
function bindecValues($decimal, $reverse=false, $inverse=false) {
$bin = decbin($decimal);
if ($inverse) {
$bin = str_replace("0", "x", $bin);
$bin = str_replace("1", "0", $bin);
$bin = str_replace("x", "1", $bin);
}
$total = strlen($bin);
$stock = array();
for ($i = 0; $i < $total; $i++) {
if ($bin{$i} != 0) {
$bin_2 = str_pad($bin{$i}, $total - $i, 0);
array_push($stock, bindec($bin_2));
}
}
$reverse ? rsort($stock):sort($stock);
return implode(", ", $stock);
}
?>
Happy coding
Remember that integers are stored in binary - so each of these flags (Allergy = 1) etc. will correspond to a single bit being true or false in the binary representation of the sum.
For example, 2689 in binary is 0000 1010 1000 0001 which, if you think of it as an array of bits, where the least significant bit (right most in that array) is the least significant flag (allergy) then we can easily see that the first (allergy), eighth (gen. medicine), tenth (hematology) and twelfth (obs) slots of the array are marked with a 1 for true.
The largest value in your array of flags is 24th bit in a 32 bit integer. You could define up to 8 more flags in this system before having to use a larger integer.
Since all your numbers seem to be powers of two, you just need to store the input value in a long enough integer to hold it, then bit mask.
if( value & 1 ) then 1 was part of the selection
if( value & 2 ) then 2 was part of the selection
if( value & 3 ) then 3 was part of the selection
and so on
I'm not familiar with bitwise operators, but I have seem them used to store simple settings before.
I need to pass several on/off options to a function, and I'd like to use a single integer for this. How can I go about setting and reading these options?
You sure can do it in PHP.
Let's say you have four booleans you want to store in a single value. That means we need four bits of storage space
0000
Each bit, when set individually, has a unique representation in decimal
0001 = 1 // or 2^0
0010 = 2 // or 2^1
0100 = 4 // or 2^2
1000 = 8 // or 2^3
A common way to implement this is with bit masks to represent each option. PHP's error levels are done this way, for example.
define( 'OPT_1', 1 );
define( 'OPT_2', 2 );
define( 'OPT_3', 4 );
define( 'OPT_4', 8 );
Then when you have an integer that represents 0 or more of these flags, you check with with the bitwise and operator which is &
$options = bindec( '0101' );
// can also be set like this
// $options = OPT_1 | OPT_3;
if ( $options & OPT_3 )
{
// option 3 is enabled
}
This operator works as such: only bits that are set in both operands are set in the result
0101 // our options
0100 // the value of OPT_3
----
0100 // The decimal integer 4 evaluates as "true" in an expression
If we checked it against OPT_2, then the result would look like this
0101 // our options
0010 // the value of OPT_2
----
0000 // The decimal integer 0 evaluates as "false" in an expression
It works pretty much the same way in both languages, a side by side comparison:
C:
#include <stdio.h>
#include <stdint.h>
#define FLAG_ONE 0x0001
#define FLAG_TWO 0x0002
#define FLAG_THREE 0x0004
#define FLAG_FOUR 0x0008
#define FLAG_ALL (FLAG_ONE|FLAG_TWO|FLAG_THREE|FLAG_FOUR)
void make_waffles(void)
{
printf("Yummy! We Love Waffles!!!\n");
}
void do_something(uint32_t flags)
{
if (flags & FLAG_TWO)
make_waffles();
}
int main(void)
{
uint32_t flags;
flags |= FLAG_ALL;
/* Lets make some waffles! */
do_something(flags);
return 0;
}
PHP:
<?php
define("FLAG_ONE", 0x0001);
define("FLAG_TWO", 0x0002);
define("FLAG_THREE", 0x0004);
define("FLAG_FOUR", 0x0008);
define("FLAG_ALL", FLAG_ONE|FLAG_TWO|FLAG_THREE|FLAG_FOUR);
function make_waffles()
{
echo 'Yummy! We Love Waffles!!!';
}
function do_something($flags)
{
if ($flags & FLAG_TWO)
make_waffles();
}
$flags |= FLAG_TWO;
do_something($flags);
?>
Note, you don't absolutely need to use constants, I just use them out of habit. Both examples will run, I compiled the C version via gcc -Wall flags.c -o flags. Change flags in either example to anything but FLAG_TWO or FLAG_ALL and (sadly) no waffles will be made.
In the C version, you don't have to tickle the preprocessor, it could quite easily be an enum, etc - that's an exercise for the reader.
quote
"the idea is not good, really. you would better pass few boolean. if you want use bitwise then
function someFunc($options)
{
if ($options & 1 != 0)
//then option 1 enabled
if ($options & (1 << 1) != 0)
//then option 2 enabled
if ($options & (1 << 2) != 0)
//then option 3 enabled
}
"
What you have done would be okay if you were checking for a single value, although not optimal, so checking that a bit is enabled, but lets say we wanted to be able to match any, or exact we could have the following methods
function matchExact($in, $match) { // meets your criterion, as would a switch, case, but ultimately not suited for use with flags
return $in === $match;
}
function matchAny($in, $match) { // meets original criterion with more lexical name however it returns true if any of the flags are true
return $in |= $match;
}
if you then wanted to expand upon this by having specific actions only happening if bit x,y,z was enabled then you could use the following
function matchHas($in, $match) { // more bitwise than === as allows you to conditionally branch upon specific bits being set
return $in &= $match;
}
I also think if you are doing what was done in the above quote, flags may not be the best idea, exact values might be better, which does have the benefit of allowing more discreet actions. (0-255) for 8-bit over 8 distinct flags
The whole reason flags work so well is because in base 2 "8" does not contain "4", and "2" does not contain "1".
________________________
|8|4|2|1|Base 10 Value |
------------------------
|1|1|1|1|15 |
|1|1|1|0|14 |
|1|1|0|1|13 |
|1|1|0|0|12 |
|1|0|1|1|11 |
|1|0|1|0|10 |
|1|0|0|1|9 |
|1|0|0|0|8 |
|0|1|1|1|7 |
|0|1|1|0|6 |
|0|1|0|1|5 |
|0|1|0|0|4 |
|0|0|1|1|3 |
|0|0|1|0|2 |
|0|0|0|1|1 |
|0|0|0|0|0 |
------------------------