# 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.

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

## Decimal to Binary

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

1. The largest power of 2 that works is [itex]2^6=64[/itex].
2. [itex]121-64= 57[/itex]. Binary number so far: 10000000
3. Next power of 2 that works is [itex]2^5=32[/itex].
4. [itex]57-32=25[/itex]. Binary number so far: 11000000
5. Next power of 2 that works is [itex]2^4=16[/itex].
6. [itex]25-16=9[/itex]. Binary number so far: 11100000
7. Next power of 2 is [itex]2^3=8[/itex].
8. [itex]9-8=1[/itex]. Binary number so far: 11110000
9. Next power of 2 smaller that works is [itex]2^0=1[/itex].
10. [itex]1-1=0[/itex]. You're done. Don't forget to write your zeros for the powers of 2 you skipped.

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

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

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.

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

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

Decimal Conversion [itex]3*16^2+11*16^1+10*16*0=954[/itex]

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

## 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
• 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 %

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

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

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

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

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

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

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?