What does >>= do in this example?
byte fsr = 2;
fsr >>= 2;
I came across it here: https://github.com/sparkfun/MMA8452_Accelerometer/blob/master/Firmware/MMA8452Q_BasicExample/MMA8452Q_BasicExample.ino
Bitshifting shifts the binary representation of each pixel to the left or to the right by a pre-defined number of positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to the left) or dividing (when shifting to the right) the number by 2.
The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted.
<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .
The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.
It does this:
fsr = fsr >> 2;
fsr >>= 2;
is
fsr = fsr >> 2;
In Bitwise Context, two bit places to the right is being shifted.
In Arithmetic context, the number in fsr is being divided by 2^2(4);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With