CS485G Spring 2015 9
7. One can use the Boolean operators in C and apply them to any inte-
gral data type: char, short, int, long, long long.
8. Don’t confuse these operators with logical operators &&, ||, and !.
In C, 0 is understood to mean false, and any other value is true. The
logical operators always return either 0 or 1. They use short-circuit
semantics.
11 Shifting operators
1. Left shift: x << y Left-shifts bits in x by y positions; new positions
on the right are filled with 0. Warning: In C (and Java), if y is equal
to or greater than the number of bits n in the type of x, the shift
distance is y mod n. So shifting a 32-bit integer by 34 bits only shifts
it 34 mod 32 = 2 bits.
2. Right shift: x >> y Right-shifts bits in x by y positions; new posi-
tions on the left are filled with the sign bit. The same warning ap-
plies.
12 Encoding integers
1. Given n bits, there are 2
n
possible integers.
2. In unsigned form, these range from 0 (represented as all 0 bits) to
2
n
− 1 (represented by all 1 bits).
3. In signed form, these range from −2
n−1
to 2
n−1
− 1.
(a) 0 is represented by all 0 bits.
(b) The most significant bit is 0 for positive numbers. The largest
positive number has n − 1 bits set, representing 2
n−1
− 1.
(c) The most significant bit is 1 for negative numbers. The smallest
negative number has only that bit set, representing −2
n−1
.
(d) To negate a number (either a positive or negative one), invert all
the bits and add 1 to the result, ignoring overflow.
(e) 4-bit examples
i. 0000
2
= 0
ii. 0110
2
= 6
iii. 1010
2
= −6
Comentarios a estos manuales