Number Representations

Signed Integers

We’ve only represented 15 of our 16 available numbers!

DecimalPositiveNegative
000000000
100011111
200101110
300111101
401001100
501011011
601101010
701111001
810001000
91001(same as -7!)NA
101010(same as -6!)NA
111011(same as -5!)NA
121100(same as -4!)NA
131101(same as -3!)NA
141110(same as -2!)NA
151111(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