CSC373/406: Integers: Bits! How Programs are Stored [6/9] Previous pageContentsNext page

Storing programming statements

Storing data

Using printf format strings to look at underlying representations of data

Conversion between decimal, binary, and hex

In base 10, the nth digit from the right in the number represents the number of 10^n 's in the number

Example: 2538

DigitOffset from the rightRepresents
808 * 10^0 = 8
313 * 10^1 = 30
525 * 10^2 = 500
802 * 10^3 = 2000

So the number is 8 + 30 + 500 + 2000 = 2538.

Converting from other bases to base 10 can be done in the same way. For example, in binary, each column represents a power of 2.

Example: 1001101

DigitOffset from the rightRepresents
101 * 2^0 = 1
010 * 2^1 = 0
121 * 2^2 = 4
131 * 2^3 = 8
040 * 2^4 = 0
050 * 2^5 = 0
161 * 2^6 = 64

So the number is 1 + 0 + 4 + 8 + 0 + 0 + 64 = 37

Example of conversion from hex

Example: 0xA203

DigitOffset from the rightRepresents
303 * 16^0 = 3
010 * 16^1 = 0
220 * 16^2 = 512
a310 * 16^3 = 40960

So the number is 3 + 0 + 512 + 40960 = 41475

Previous pageContentsNext page