Binary Decimal And Hexadecimal

Existing Tutorials

Again, there are existing tutorials that touch on this topic.

Sigma's Learn Assembly in 28 days, day3.

Cobb's z80 Assembly, Data and Numbers.

If you still don't understand number bases, read on.

The Binary Base

The most basic counting system you can get, it is based on just 1's and 0's. Each 1 or 0 is a bit, and 8 bits make up a byte. Counting in binary is related to counting in decimal. Here's how: You keep counting in the units digit (farthest right) until there are no more number symbols to count. Then you increment the next digit and start over again. Here's an example:
binary 0,1,10,11,100,101,110
decimal 0,1,2,3,4,5,6

The Decimal Base

Hopefully you know how this base works. It's what you use everyday. The digits are 0,1,2,3,4,5,6,7,8, and 9.

The Hexadecimal Base

Counting in ones and zeros can be a pain, and converting between decimal and binary is no better. So, to help sort ease the pain of programmers we'll use the hexadecimal base. This base uses the digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F. Again, counting similar binary and decimal.

Conversions of Binary, Decimal, and hexadecimal

Binary to Decimal

The easier of the two, what you do is take each digit times 2 to the power of the column it is in and add it all together. Please note that the farthest right digit is the 0th digit, not the 1st digit. Here's and example:
Binary Number 1001
Conversion to Decimal $1*2^3 + 0*2^2 + 0*2^1 + 1*2^0=9$

Decimal to Binary

Converting back to binary is slightly more complicated. What you have to do is figure out the largest power of 2 ($2^n$) that is smaller or equal to than your number. Write the nth digit as a 1 and continue. You then subtract the power of 2 from the number and repeat until the decimal number equals 0 (take the result of the subtraction as the new number). If you skip over a power of two, don't forget to write a 0.
Some Useful Powers of 2 $2^0=1$,$2^1=2$,$2^2=4$,$2^3=8$,$2^4=16$,$2^5=32$,$2^6=64$,$2^7=128$,$2^8=256$,$2^9=512$,$2^10=1024$,$2^11=2048$,$2^12=4096$,$2^13=8192$,$2^14=16384$,$2^15=32768$
Decimal Number 121
Conversion to Binary
1. The largest power of 2 that works is $2^6=64$.
2. $121-64= 57$. Binary number so far: 10000000
3. Next Power of 2 that works is $2^5=32$.
4. $57-32=25$. Binary number so far: 11000000
5. Next Power of 2 that works is $2^4=16$.
6. $25-16=9$. Binary number so far: 11100000
7. Next Power of 2 is $2^3=8$.
8. $9-8=1$. Binary number so far: 11110000
9. Next Power of 2 smaller that works is $2^0=1$.
10. $1-1=0$. You're done. Don't forget to write your zeros for the powers of 2 you skipped.
Final answer: 11110001

Binary to Hexadecimal

A rather simple task, though slightly harder than converting binary to decimal. You do the same thing except make sure that when adding you don't go onto the next digit until the previous digit is larger than F (15 in decimal).
Binary number 11110011
Conversion to Hexadecimal $1*2^7+1*2^6+1*2^5+1*2^4+0*2^3+0*2^2+1*2^1+1*2^0=F3$

You'll probably be asking "how is that easier than converting binary to decimal?"

Here's your answer: If you haven't notice yet, but each digit in hexadecimal is represented by 4 digits in binary. What does this mean? It means that you only have to compute hexadecimal a nibble (4 bits) at a time. So:
Previous number split up into nibbles 1111 0011
First nibble (right one) 0011=3
Second nibble 1111=F

Hexadecimal to Binary

Another simple task, again we'll use the notion of nibbles to help us. Take each hexadecimal digit and convert it to decimal (A=10,B=11,C=12,D=13,E=14,F=15). Then convert that number to binary.
Hexadecimal Number E7
First digit 7
Decimal equivalence 7
Binary conversion 7->0111
Second digit E
Decimal equivalence 14
Binary conversion 14->1110
Final answer 11100111

Hexadecimal to Decimal

Simple. Just take the decimal equivalence of the hexadecimal digit, times it by $16^n$ where n equals the location the digit is in (0 being the farthest right), and add them all up.
Hexadecimal Number 3BA
Decimal Conversion $3*16^2+11*16^1+10*16*0=954$

Decimal to Hexadecimal

Similar to converting from binary to hexadecimal. Just don't forget you need to add up to F (15) before going onto the next digit.
Decimal Number 68
Hexadecimal Conversion $6*A^1+8*A^0=44$

Setting up Windows Calculator to convert for you

If you didn't already know, you can use Windows Calculator to convert to certain bases. Here's how you set it up:

  • Go to view
  • Change to Scientific mode (as opposed to the default "Standard" mode)
  • Choose the base of the number you want to convert by selecting either hex, dec, oct, or bin.
  • Determine how large of a number you're going to input, and adjust the panel next to it accordingly:

byte: Fits in 8 bits
word: Fits in 16 bits
Dword: Fits in 32 bits
Qword: Fits in 64 bits

  • Type in your number
  • Click on the base you want to convert to.
  • The number will be changed to the equivalent number for that base

Common notation

How do you distinguish one number base from other base numbers? The compiler has 2 ways:

Denote before

Use a symbol to denote the base of the number.
binary %
hexadecimal $

Note that decimal has no pre-denote form.

Denote after

Use a symbol after to denote the base of the number.
binary b
decimal d
hexadecimal h

Note that if you're using this method with hexadecimal, it is important that the first digit is not a letter (A,B,C,D,E, or F). If necessary, add a 0 to the front.

Just to save you some trouble, if you don't put a denotation for a number the compiler will assume that it is a decimal number.

Review

Here are some questions to make sure that you really understand this. If not, perhaps you should re-read this section.

Convert the following numbers

Don't use a base converter, but a calculator is alright if you need to multiply certain things.

Binary to Decimal

1. 1110
2. 11001001
3. 110101011000
4. 1111
5. 10001000

Binary to Hexadecimal

1. 1001
2. 10101010
3. 10011101
4. 1100
5. 10000110

Decimal to Binary

1. 10
2. 36
3. 57
4. 184
5. 376

Decimal to Hexadecimal

1. 91
2. 100
3. 852
4. 4533
5. 424

Hexadecimal to Binary

1. 10
2. AB
3. 8F
4. 3E
5. B7

Hexadecimal to Decimal

1. 4
2. F8
3. EB
4. D1
5. 97

Denotations

Identify the following numbers as binary, decimal, or hexadecimal according the the denotation.

1. %10001110
2. 1011
3. $100101
4. 10h
5. 1111b
6. 11
*7. AEh
8. 54d
9. %10010010011
10. 100000000001h

*What's wrong with this?

For solutions, see this page.

Conclusion

It takes a lot of practice to get good at converting between bases. Although you could just use Windows calculator to convert between bases, it's still useful to know how it's done by hand. If used correctly, bases and converting between bases will make programming as well as understanding other people's code a lot easier.

Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.