Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitshift and equals together

Tags:

c

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

like image 607
Scott Goldthwaite Avatar asked May 23 '13 01:05

Scott Goldthwaite


People also ask

What does a Bitshift do?

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.

How does right shift and left shift work?

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.

What is << in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does >> mean in Java?

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.


2 Answers

It does this:

fsr = fsr >> 2;
like image 50
gzm0 Avatar answered Nov 15 '22 06:11

gzm0


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);

like image 30
Barath Ravikumar Avatar answered Nov 15 '22 06:11

Barath Ravikumar