932 B
932 B
Chapter 5. Bit manipulation
5.1. Two's complement and negative numbers
Positive numbers are represented as themselves, negative numbers as the two's complement of its absolute value, with a 1 in its sign bit, indicating it's negative.
The two's complement of an N-bit number (N: # bits used for the number, excluding the sign bit), is the complement of the number with respect to 2N.
The binary representation of -K as a N-bit number is concat(1, 2N-1 - K).
5.2. Arithmetic vs. Logical right shift
Arithmetic right shift: division by two. Logical right shift: 'shifting the bits'.
5.2.1. Logical shift
Indicated with a >>> operator, all bits are shifted, sign bit too.
- -75: 10110101
- 90: 01011010
5.2.2. Arithmetic shift
The sign bit is kept, and all bits (sign bit too) are shifted.
- -75: 10110101
- -38: 11011010