z80:Binary, Decimal, and Hexadecimal

From Learn @ Cemetech
Jump to navigationJump to search

Existing Tutorials

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

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 <math>1*2^3 + 0*2^2 + 0*2^1 + 1*2^0=9</math>

Decimal to Binary

Converting back to binary is slightly more complicated. What you have to do is figure out the largest power of 2 (<math>2^n</math>) 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 <math>2^0=1</math>,<math>2^1=2</math>,<math>2^2=4</math>,<math>2^3=8</math>,<math>2^4=16</math>,<math>2^5=32</math>,<math>2^6=64</math>,<math>2^7=128</math>,<math>2^8=256</math>,<math>2^9=512</math>,<math>2^{10}=1024</math>,<math>2^{11}=2048</math>,<math>2^{12}=4096</math>,<math>2^{13}=8192</math>,<math>2^{14}=16384</math>,<math>2^{15}=32768</math> Decimal Number 121 Conversion to Binary

  1. The largest power of 2 that works is <math>2^6=64</math>.
  2. <math>121-64= 57</math>. Binary number so far: 10000000
  3. Next power of 2 that works is <math>2^5=32</math>.
  4. <math>57-32=25</math>. Binary number so far: 11000000
  5. Next power of 2 that works is <math>2^4=16</math>.
  6. <math>25-16=9</math>. Binary number so far: 11100000
  7. Next power of 2 is <math>2^3=8</math>.
  8. <math>9-8=1</math>. Binary number so far: 11110000
  9. Next power of 2 smaller that works is <math>2^0=1</math>.
  10. <math>1-1=0</math>. 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 <math>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</math>

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.

  1. Hexadecimal Number E7
  2. First digit 7
  3. Decimal equivalence 7
  4. Binary conversion 7->0111
  5. Second digit E
  6. Decimal equivalence 14
  7. Binary conversion 14->1110
  8. Final answer 11100111

Hexadecimal to Decimal

Simple. Just take the decimal equivalence of the hexadecimal digit, times it by <math>16^n</math> where n equals the location the digit is in (0 being the farthest right), and add them all up.

Hexadecimal Number 3BA
Decimal Conversion <math>3*16^2+11*16^1+10*16*0=954</math>

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 <math>6*A^1+8*A^0=44</math>

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.