Number Representations


Signed Integers
We’ve only represented 15 of our 16 available numbers!
| Decimal | Positive | Negative |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 1111 |
| 2 | 0010 | 1110 |
| 3 | 0011 | 1101 |
| 4 | 0100 | 1100 |
| 5 | 0101 | 1011 |
| 6 | 0110 | 1010 |
| 7 | 0111 | 1001 |
| 8 | 1000 | 1000 |
| 9 | 1001(same as -7!) | NA |
| 10 | 1010(same as -6!) | NA |
| 11 | 1011(same as -5!) | NA |
| 12 | 1100(same as -4!) | NA |
| 13 | 1101(same as -3!) | NA |
| 14 | 1110(same as -2!) | NA |
| 15 | 1111(same as -1!) | NA |
two’s complement
Overflow and Underflow
If you exceed the maximum value of your bit representation, you wrap around or overflow back to the smallest bit representation.
If you go below the minimum value of your bit representation, you wrap around or underflow back to the largest bit representation.

Casting
What happens at the byte level when we cast between variable types? The bytes remain the same! This means they may be interpreted differently depending on the type.
C will implicitly cast the signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.
Truncating Bit Representation
If we want to reduce the bit size of a number, C truncates the representation and discards the more significant bits.
unsigned int x = 128000;
unsigned short sx = x;
unsigned int y = sx;
What happens here? Let’s look at the bits in x (a 32-bit unsigned int), 128000:
0000 0000 0000 0001 1111 0100 0000 0000
When we cast x to a short, it only has 16-bits, and C truncates the number:
1111 0100 0000 0000
This is 62464! Unsigned numbers can lose info too. Here is what y looks like:
0000 0000 0000 0000 1111 0100 0000 0000// still 62464
readings
Map 256 Glitch To keep a Boeing Dreamliner flying, reboot once every 248 days Comair/Delta airline debacle caused by the overflow of 16-bit pointer Why Gandhi Is Such An Asshole In Civilization CVE-2019-3857