z80:Binary, Decimal, and Hexadecimal
Contents
Existing Tutorials
Again, there are existing tutorials that touch on this topic.
- Sigma's Learn Assembly in 28 days, Day 3.
- 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
Decimal to Binary
Converting back to binary is slightly more complicated. What you have to do is figure out the largest power of 2 () 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 ,,,,,,,,,,,,,,, Decimal Number 121 Conversion to Binary
- The largest power of 2 that works is .
- . Binary number so far: 10000000
- Next power of 2 that works is .
- . Binary number so far: 11000000
- Next power of 2 that works is .
- . Binary number so far: 11100000
- Next power of 2 is .
- . Binary number so far: 11110000
- Next power of 2 smaller that works is .
- . 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
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 where n equals the location the digit is in (0 being the farthest right), and add them all up.
Hexadecimal Number 3BA
Decimal Conversion
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
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
- 1110
- 11001001
- 110101011000
- 1111
- 10001000
Binary to Hexadecimal
- 1001
- 10101010
- 10011101
- 1100
- 10000110
Decimal to Binary
- 10
- 36
- 57
- 184
- 376
Decimal to Hexadecimal
- 91
- 100
- 852
- 4533
- 424
Hexadecimal to Binary
- 10
- AB
- 8F
- 3E
- B7
Hexadecimal to Decimal
- 4
- F8
- EB
- D1
- 97
Denotations
Identify the following numbers as binary, decimal, or hexadecimal according the the denotation.
- %10001110
- 1011
- $100101
- 10h
- 1111b
- 11
- AEh*
- 54d
- %10010010011
- 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.