There is a sample final exam.
Tuesday, November 19, 2002
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
int unsigned int
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
-8 1000 8
-7 1001 9
-6 1010 10
-5 1011 11
-4 1100 12
-3 1101 13
-2 1110 14
-1 1111 15
start with the
positive number, 5: 0101
flip all bits: 1010
... and add one: 1
----
-5: 1011
decimal bit decimal
equivalent pattern equivalent
if int if unsigned int
2 0010 2
-5 1011 11
--- ---- ---
-3 1101 13
You also can see why your friendly compiler warns you when you mix (signed) int-s with unsigned int-s: in a 4-bit system, an unsigned 13 would be equal to a (signed) -3! Here is a demo illustrating this matter for 32-bit numbers.
Another matter that this same demo illustrates is that C++ provides operations on the bits such as the >> for shifting to the right (yes, the same symbol that is used for the input operator) and the & for “bitwise and” used in the printing of the bits:
// showing the actual bits
cout << "unsigned int = ";
for (int b = 31; b >= 0; b--)
cout << ((ui >> b) & 1);
© 2002 Karl Winklmann