If you have some variable (on the stack) and you left or right bit shift beyond its end what happens?
i.e.
byte x = 1;
x >> N;
What if x is a pointer to memory cast to a byte and you do the same thing?
byte* x = obtain pointer from somewhere;
*x = 1;
*x >> N;
When shifting left, the most-significant bit is lost, and a 0 bit is inserted on the other end. The left shift operator is usually written as "<<".
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. The result is not an lvalue.
To multiply a number, a binary shift moves all the digits in the binary number along to the left and fills the gaps after the shift with 0: to multiply by two, all digits shift one place to the left. to multiply by four, all digits shift two places to the left.
The left shift operator ( << ) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.
It does not (necessarily) become zero. The behavior is undefined (C99 §6.5.7, "Bitwise shift operators"):
If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
(C++0x §5.8, "Shift operators"):
The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.
The storage of the value being shifted has no effect on any of this.
I think you're confused about what bitshifts do. They are arithmetic operators equivalent to multiplication or division by a power of 2 (modulo some weirdness about how C treats negative numbers). They do not move any bits in memory. The only way the contents of any variable/memory get changed are if you assign the result of the expression back somewhere.
As for what happens when the righthand operand of a bitshift operator is greater than or equal to the width of the type of the lefthand expression, the behavior is undefined.
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