What's the correct way to handle two distinct values being stored in one byte of data. I have a byte that contains two nibbles each containing their own data. I want to read the top nibble and the bottom nibble into their own variables.
11110000 = High 4 bits throttle, to be read into $throttle, and should be a value from 0 to 15.
00001111 = Low 4 bits brake, to be read into $brake, and should be a value from 0 to 15.
Don't forget, drivers can apply the throttle and the brake at the same time, so you might get a value like 11000111. I've myself come up with a solution for the high 4 bits, and it's as simple as pushing the lower 4 bits out of the way with the >> (bit shift right) operator 4 times. $Throttle = $ThrBrk >> 4, but as I can't do that in one move for the lower four bits it looks kinda bad in my source code.
Use ANDoperators for both and shift the top nibble four bits to the right.
$brake = $value & 0x0F;
$throttle = ($value & 0xF0) >> 4;
Check out the & operator, which is a bitwise AND. To get the first (least significant bit), do this:
$lsb = $bits & 1;
So, to get the whole "nibble":
$break = $bits & 15;
Related
I've spent the last hour going around in circles on this so it's finally time to ask for help. I have two binary 8-bit words such as these:
$words[0]="00000101";
$words[1]="01111001";
I want to take the two right bits (01) of $words[0] and append it to the start of $words[1] to make 0101111001 = 377 in decimal.
The easiest way would be to use PHPs string functions to do a substr() but I'd rather learn how to do it using bitwise operators as I need to do this for lots of other examples as well.
What I thought I'd do is to to do 00000101 AND 0x03 to give me 64 and then shift the bits 8 places to the right so that I can add them to $words[1] using OR.
In code that would be:
($array[0] & 0x03 << 8) | $array[1]
but I just get the value of $array[1] back. It seems that it's not possible to shift a value to left more than 8 bits as it gets set to zero (which makes sense).
So, how can accomplish what I want to do ?
You didn't use binaries. Binaries starts with 0b... without "".
This code works:
$words[0]=0b00000101;
$words[1]=0b01111001;
$last = $words[0] & 0b11;
$shift = $last << 8;
$newWord = $shift | $words[1];
echo $newWord;
Or in short form:
echo ($words[0] & 0b11) << 8 | $words[1];
I use the bitwise & 0b11 to get the last two digits from the first word, shift it with <<8 and use the bitwise or |.
For the last | you can use + instead, if you want.
EDIT:
If you want the result as binary just use decbin:
echo decbin($newWord);
I have a 16-bit number, like 0x789A and I want to change the 2 most significant bits to 10 (setting the highest and clearing the second-highest) to end up with 0x989A.
I know that I can set the first bit using n | 0x8000 and I can unset the second bit (in php) using n & ~0x4000 but is there a way to "cast" the two bits to 10 in one operation without affecting the lesser bits?
Similarly, I have another 16-bit number, like 0xABCD that I want to change the byte value of the first byte to a different hex value, for instance 5 so that I end up with 0x5BCD. Is there a way to set bytes using bitwise operations? I'm not even sure how to begin for this.
This will set the 2 most significant bits to binary 10:
0x8000 | ($n & 0x3fff)
This will set the most signigicant byte to hex 5:
0x5000 | ($n & 0x0fff)
The general idea is that you use 0 bits in the mask to specify the bits you want to replace, 1 to specify the bits to keep, and then OR in the replacement.
Do it exactly as you had, just do it two bits at a time, e.g
(0x789A & ~0xC000) | 0xC000
0111100010011010 0x789A
& 0011111111111111 ~0xC000 ---clear the two relevant bits
------------------
= 0011100010011010 0x389A
| 1000000000000000 0xC000 --- set their new values
------------------
= 1111100010011010 0xF89A
The PHP imagecolorat() function can be used to get the RGB values of an image pixel, as demonstrated in the documentation:
$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
I don't understand the last three lines at all; I know that they return the correct values, but I cannot figure out how the >> and & operators work together to do so. Could someone please explain?
For reference, when $rgb = 9750526, the RGB values turn out to be(148,199,254).
Thanks for any responses.
& and >> are two different bitwise operators (Bitwise operators in PHP). They work with the bit representation of the values, which gives us a few useful (and fast!) possibilities.
>> is right shift. If you right shift a value, you're moving all the bits to the right in the representation. If you have "11100" and right shift it by two places, you're left with "111" (the bits shifted off on the right end disappears). The number on the right side of >> indicates the number of bits to shift.
& is bitwise and. If you have two values represented as 111000 and 101011, and decide to bitwise and them, you'll end up with 101000 (the bits where the same bits in both values are set will be 1, otherwise 0).
We now have what we need to make sense of the code above. $rgb contains an integer value (the value is not interesting, but the bit pattern is), which represents 24 bits (32 bits for RGBA, but we'll ignore that here). The value consists of 8 bits for red, 8 bits for green and 8 bits for blue. We're however not interested in what number those 24 bits represent together, but what the separate R, G and B values are.
Our value represent the following bits for the colors:
rrrrrrrr gggggggg bbbbbbbb
If we shift this bitmask with >> 16, we'll get:
rrrrrrrr
This is what >> 16 does, and leaves us with just the bits representing the red color. As we've now moved the bits all the way down, the value represents a number in [0, 255]. We then add & 255 (which here is written as 0xFF in hexadecimal notation) to remove any stray bits above the 8 we're interested in. We'll get to how that happens now, when we look at how we get the G value:
Our value still bravely represents the different colors:
rrrrrrrr gggggggg bbbbbbbb
If we shift this bitmask 8 places with >> 8, we'll get:
rrrrrrrr gggggggg
But wait! This is not just the bits that represents g, but also the bits that represent r! This is not the value we're after (as we just want g's). We were lucky in the previous example since we didn't have any bits set above r, but this time we've really got our work cut out for us. But we know about &, and it's time to see it actually to it's stuff.
We now have:
rrrrrrrr gggggggg
And then we apply & 255 (0xFF in your code), which is representing in a bit value as:
00000000 11111111
Since & only keeps the bits which are set in both operands, we'll end up with just the g values:
gggggggg
The bits that represented red in the shifted value are now 0, and we're left with only the bits representing the green of the original color. As they've been shifter into the same 8 bits that represent [0, 255], we've got our value yet again.
The last value is easier, as it's already located where we want it: in the bits representing [0, 255]. We can just do the & operation here, and get out on the other side:
rrrrrrrr gggggggg bbbbbbbb &
00000000 00000000 11111111
Which ends up with:
bbbbbbbb
And we've got our blue value as well.
Hope that explains it!
>> means bit-wise right-shift, so the bits are moved n (16 and 8 respectively) positions to the right:
R = 1 Byte = 8 Bits
RRGGBB >> 16 = 0000RR
RRGGBB >> 8 = 00RRGG
The bit-wise AND operation & is used to remove remaining stuff that we don't want in the result, by using a mask that is zero everywhere except in the part that should stay:
00RRGG & 0000FF = 0000GG
Let's try:
$rgb = 9750526;
var_dump(sprintf('%032b', $rgb));
$rgb = 00000000100101001100011111111110 //integer is 32 or 64 bit - b part is last 8bits
you need only 3rd, 2nd and 1st byte, so >> (shift) them
and you get binary
$rgb >> 16 = string(32) "00000000000000000000000010010100" //r part is last 8 bits
$rgb >> 8 = string(32) "00000000000000001001010011000111" //g part is last 8 bits
& 0xFF is and operation with 11111111 so as a result you get
r = 10010100
g = 11000111
b = 11111110
and I hope those are (148,199,254) ;)
I'm guessing that the first occurrence, >>16, is shifting all the bits to the right until only red remains, then cutting off the start of the number with &0xFF. Although I can't tell the bit depth of red in that example (I would expect a multiple of 3 bytes for the color), it's sort of like this:
rrggbb = 125438 // input
>> 16 // shift right (might be 8 for this example)
0000rr = 000012 // result
& 0xFF // cut beginning
rr = 12
And similar for the other colors.
imageColorAt() returns a 24-bit number containing red, green and blue values. Red value is multiplied by 2^16, green by 2^8, and blue is left "as is".
In hexadecimal, your value is 0x94C7FE; by shifting it right 16 bits, you lose the last four nibbles and are left with 0x94. The "& 0xFF" takes the last 8 bits (and therefore it's superfluous).
Usually this shift'n'cut is done like this:
$value = imageColorAt(...) // Gets 94C7FE
$blue = $value & 0xFF; // Takes the last "FE" (254 in decimal)
$value >>= 8; // Shifts right, 94C7 remains
$green = $value & 0xFF; // Takes "C7"
$value >>= 8; // Shifts right, 94 remains
$red = $value; // Takes "94" hex, which is 148 dec.
Consider:
echo 50 >> 4;
Output:
3
Why does it output 3?
50 in binary is 11 0010, shift right by 4 yields 11 which is equal to 3.
See PHP documentation and Wikipedia.
As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right:
$a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two")
50 in binary is 110010, and the >> operator shifts those bits over 4 places in your example code. Although this happens in a single operation, you could think of it in multiple steps like this:
Step 1 - 00011001
Step 2 - 00001100
Step 3 - 00000110
Step 4 - 00000011
Since binary 11 is equal to 3 in decimal, the code outputs 3.
Arithmetic shift right.
The >> operator is called a binary right shift operator.
Shifting bits to the right 4 times is the same as dividing by two, four times in a row. The result, in this case would be 3.125. Since 50 is an int, bit shifting will return the floor of this, which is 3.
Put another way, 50 is 0b110010 in binary. Shifted 4 times we have 0b11, which is 3 in decimal.
>> is the binary right-shift operator.
Your statement shifts the bits in the numeric value 50 four places to the right. Because all integers are represented in two's complement, this equals 3. An easy way to remember this is that one shift to the right is the same as dividing by 2, and one shift to the left is the same as multiplying by 2.
It's called a right shift.
'The bits of the left operand are shifted right by the number of positions of the right operand. The bit positions vacated on the left are filled with the sign bit, and bits shifted out on the right are discarded.'
Information can be found on it here:
http://php.comsci.us/etymology/operator/rightshift.php
It shifts the bits down four places.
50 in binary is 110010.
Shifted down four places is 11, which is 3.
For your convenience, one of the fastest ways to calculate the outputted value from a bitwise shift is to multiply or divide by 2.
For example echo 50 >> 4;
Given that this is a bitwise right, it literally means that the value will be decrease, then we can get the output by divide 50 for 2 and 4 times.
echo 50 >> 4; // 50/(2*2*2*2) ~ 3.
Given that (from) 48 -> (to) 63/16(2*2*2*2), the result will be more than 2 and less than 4. Then
echo 48 >> 4; // 48/(2*2*2*2) ~ 3.
echo 63 >> 4; // 63/(2*2*2*2) ~ 3.
However, when bitwise left, the result will be totally different as it multiplies by 2 with n times:
If echo 50 << 4; // 50*(2*2*2*2) ~ 800
If echo 51 << 4; // 51*(2*2*2*2) ~ 816
Live example: https://3v4l.org/1hbJe
What does this code mean and what are other ways accomplish the same without using bit shifting?
if ($n & ($n - 1))
That formula checks to see whether a number is a power of 2 (if your condition as written is true, then the number is not a power of two).
Stated another way, your test checks to see whether there is more than one "1" bit set in the binary representation of $n. If there is zero or only one bit set, then your test will be false.
It is by far the most efficient way to determine that property.
First, this code is valid PHP, so your title is poor.
Second, the binary arithmetic going on looks something like this:
42 = 101010
&
41 = 101001
-----------
40 = 101000
Like Greg states this is the fastest way to check for a power of 2 number, but the code you've given checks to see if the number is not a power of 2. This can easily be ascertained by PHP's policy of: any non-null/non-zero value is true.
When we use ($n & ($n - 1)) then it converts $n & ($n-1) to its binary values and does binary AND operation.
Example
3 = 0011
4 = 0100
5 = 0101
3 = 0011
&
4 = 0100
------------
0
4 = 0100
&
5 = 0101
-----------
100
To check if given number is power of 2 or not we alway use formulae
($n & ($n - 1) == 0) which means ANDing of $n & $n-1 is equals to 0 or not.
Online AND Operation Calculator