Pages: [1]
Print
Author Topic: Binary and Hex refresher course  (Read 775 times)
b3lha
*
Offline Offline

Posts: 196



View Profile WWW
« on: January 20, 2009, 08:16:40 AM »

When we see a number like 1234 in our normal base10 number system, this actually represents

(1 x 1000) + (2 x 100) + (3 x 10) + (4 x 1)

As we used to say at primary school: Thousands, Hundreds, Tens and Units. Each column is a power of 10.

1000 is 103
100 is 102
10 is 101
1 is 100

1234=(1x103) + (2x102) + (3x101) + (4x100)

The same principle is used in Binary (base 2) but we use powers of two rather than 10.

128 is 27
64 is 26
32 is 25
16 is 24
8 is 23
4 is 22
2 is 21
1 is 20

A number like 154 (decimal) is encoded as follows in binary:

154(decimal)=10011010(binary)=(1x27)+(0x26)+(0x25)+(1x24)+(1x23)+(0x22)+(1x21)+(0x20)
154(decimal)=10011010(binary)=(1x128)+(0x64)+(0x32)+(1x16)+(1x8)+(0x4)+(1x2)+(0x1)

Here is how you count in binary:
DecimalBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
101010
111011
121100
131101
141110
151111

At the lowest level, everything in a computer is encoded in binary. The cells in the memory chips are either "on" or "off" - representing "1" or "0". For convenience, the memory cells are grouped into blocks of 8 called bytes and each byte has an address to identifiy it.

Computers love binary, but typing in all thoses 1's and 0's can be a little tedious for programmers, especially for larger numbers. So programmers like to use Hexadecimal (base 16). 16 is a power of 2 and therefore it is much easier to convert between binary and hex than it is to use decimal.

In hex, we have six extra digits between 9 and 10. You count from 0 to 9, then continue A to F, then 10 to 19, 1A to 1F then 20 etc. When you get to 9F, the next number is A0 and so on. When you get to FF, the next number is 100.

Converting between hex and binary is very easy because each hex digit represents exactly 4 bits of binary.
HexBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
A1010
B1011
C1100
D1101
E1110
F1111

For example, the value A531 in hex is 1010 0101 0011 0001 in binary.
Or, the other way around 0110 1001 1111 0010 in binary is 69F2 in hex.

« Last Edit: May 10, 2009, 04:54:05 PM by kochiro » Logged

See my Subaru ECU and TCU website.
http://www.alcyone.org.uk/ssm
b3lha
*
Offline Offline

Posts: 196



View Profile WWW
« Reply #1 on: January 27, 2009, 10:41:54 AM »

LEFT and RIGHT shifting.

One of the easiest operations in the decimal system is to multiply or divide by 10.

For example:

To multiply 20 by 10, you shift the digits one place left and add a zero so it becomes 200.
To divide 20 by 10, you shift the digits one place right and lose a zero so it becomes 2.

It is exactly the same in binary, except we use 2 rather than 10.

To multiply 00011000 by 2 you shift it one place left and add a zero so it becomes 00110000
To divide 00011000 by 2 you shift it one place right and lose a zero so it becomes 00001100

When you see a piece of code like:
Code:
lda     ax, 0x1122    ; suppose the value is 3200
lsr     ax                   ; divide it by 2, now it's 1600
lsr     ax                   ; divide it by 2, now it's 800
lsr     ax                   ; divide it by 2, now it's 400
lsr     ax                   ; divide it by 2, now it's 200
lsr     ax                   ; divide it by 2, now it's 100
sta     al, 0x130e     ; store the value
It is taking the value in location 0x1122 and dividing it by 32 and storing the result at 0x130e.

Logged

See my Subaru ECU and TCU website.
http://www.alcyone.org.uk/ssm
Pages: [1]
Print
Jump to: