What’s the purpose of the rotate instructions (ROL, RCL on x86)?

Rotates are required for bit shifts across multiple words. When you SHL the lower word, the high-order bit spills out into the carry. To complete the operation, you need to shift the higher word(s) while bringing in the carry to the low-order bit. RCL is the instruction that accomplishes this. High word Low word CF … Read more

Declaring 64-bit variables in C

As you supposed, 1 is a plain signed int (which probably on your platform is 32 bit wide in 2’s complement arithmetic), and so is 43, so by any chance 1<<43 results in an overflow: in facts, if both arguments are of type int operator rules dictate that the result will be an int as … Read more

Why was 1

The relevant issue is CWG 1457, where the justification is that the change allows 1 << 31 to be used in constant expressions: The current wording of 5.8 [expr.shift] paragraph 2 makes it undefined behavior to create the most-negative integer of a given type by left-shifting a (signed) 1 into the sign bit, even though … Read more

unsigned right Shift ‘>>>’ Operator in Java [duplicate]

See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19 If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (ยง15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used … Read more

Need help understanding “getbits()” method in Chapter 2 of K&R C

Let’s use 16 bits for our example. In that case, ~0 is equal to 1111111111111111 When we left-shift this n bits (3 in your case), we get: 1111111111111000 because the 1s at the left are discarded and 0s are fed in at the right. Then re-complementing it gives: 0000000000000111 so it’s just a clever way … Read more