[TUTORIAL nt89]Introduction to hex.decimal and to Hex to bin

Sab Abr 08, 2006 12:54 am

mini tutorial base per hexadecimal

Hexadecimal to Decimal and Decimal to Hexadecimal Tutorial
Introduction:
Hexadecimal, or hex for short is, as the name might suggest, a number system with a base number of 16. The number system we use daily, decimal, is based on 10.
Hexadecimal uses letters to express numbers greater than 9 in a single character.
Decimal Hex
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 a
11 b
12 c
13 d
14 e
15 f

A few things to know about hex:

Like decimal, hex can be broken down into columns.

1) Decimal -
Tens Ones =
9 3 93


There is the ones and the tens for a number up to 99.

2) Hexadecimal
!6's Ones =
c 4 228
Now, in hexadecimal there is a column for 1 - 16 and a column for 16's.



--------------------------------------------------------------------------------

Hexadecimal to Decimal:
Step 1) Multiply the value of the 16's column by 16.
eg) e3
e = 14
14 * 16 = 224
Step 2) Add the value of the ones column to the total of the 16's column. The total is the hex number in decimal.
224 + 3 = 227
e3 = 227

Try these ones:
1) a5
2) 3e
3) 6a
4) ea
5) ff

Answers



--------------------------------------------------------------------------------


Decimal to Hexadecimal:
Step 1) Divide the total number by 16. This will give you the value of the 16's column
(Don't forget to change the value to letter (a - f) if its greater then 9)
eg) 234
234 / 16 = 14 r 10
14 = e
2) The remainder is the ones column. Put them together to make the answer.
10 = a
234 = ea

Try these to see if you got the hang of it.
1) 201
2) 95
3) 191
4) 253
5) 121


Introduction
Hex is a number system that is pretty unheard of outside of the computer programming world, but is extremely useful. If you're learning C or assembly (or even QB, I suppose!), knowing the hexadecimal (hex) number system can make life a lot easier for you.


--------------------------------------------------------------------------------

Base Systems
As humans, our native number system is base 10. That is, we count in tens -- we have 10 unique digits in our number system. A lot of people think that we've adopted this base system because... well... we have 10 fingers! There isn't really any other reason why we couldn't have evolved with a different number of fingers... then we would probably think it normal to use a different base system.

Hexadecimal (6 and 10) is base 16. In hex, there are 16 unique digits! How do we show a number higher than 9 by only using 1 digit though? The conventional method is to use A to F for the numbers 10 to 15. So the number series goes... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. What about numbers higher than 15? Think about how we manage it in the base 10 system - we keep a 'tens' column that increments each time we surpass a 9: 7, 8, 9, 10, 11, 12, ... The exact same thing happens in hex: 9, A, B, C, D, E, F, 10, 11, 12... 1F, 20, 21, 22... etc.


--------------------------------------------------------------------------------

What's so special about hexadecimal?
So why is this magical hexadecimal system used in computers? Why not base 12 or base 20?

Let's take a look at a binary number for a moment. Let's say... the number 15...


00001111

We can represent this number in hexadecimal as: 0F (or, 0x0F if we're programming in C). Now what about the number... 240?


11110000

And we represent this number in hex as F0 (0xF0). Aha, we see a pattern here! We can represent an entire byte (from 0 to 255) by using 2 hexadecimal digits, but the major significance of this is that each digit of our hexadecimal number represents the nibbles in a byte (a nibble being half a byte).

This is the reason hexadecimal is used so often in computer programming; we can access each nibble quite easily. A little example of where this is handy is with bit-wise operators. Let's say we want to totally clear the lower nibble of a byte - for this, we'd use the AND operator (written as & in C). This is how we'd do it using the decimal number system:

mybyte &= 240;

240? What the heck is that? It's not immediately obvious why we've used an AND operator with the byte -- it could mean anything. But if we used hexadecimal:

mybyte &= 0xF0;

Now we can figure this out on paper a lot more easily. 0xF0 obviously means that the upper nibble is 1111 and the lower nibble is 0000. If you're up to scratch with your bit-wise operators, it's pretty obvious that using an AND with this will clear the bottom nibble and keep the upper nibble in tact.


--------------------------------------------------------------------------------

Conclusion
So, in summary: the hexadecimal number system is useful because it allows us to represent each nibble in a byte more easily. This tutorial hasn't been a great help to understanding why hexadecimal is used so much -- you'll figure that out from using it a lot when you write your programs.



tutorial for bins to hex

Data inside a computer, as well as on the board used in this tutorial, is exchanged among the various components by means of metallic conductors called data lines. A group of data lines is called a data bus.

Each data line carries a unit of data called a bit. A bit can be on or off. On is typically considered to be 5 volts, and off is considered to be 0 volts, although modern systems often use lower on voltages to decrease power consumption.

Data can be represented on paper as a series of ones and zeros. A one means a bit is on, and a zero means it is off. A group of 8 bits is called a byte. A byte with a value of 0 would be represented as 00000000. Non-zero bytes can be any combination of 1s and 0s. 01100010 will be used as an example here. In the C language, a byte is called a character and is abbreviated char.

When data is represented as a series of ones and zeros, it is said to be a binary representation, or to have a base of 2 because it uses 2 digits.

The left-end bit of a number represented in binary is called the most significant bit, abbreviated msb, and the right-end bit is called the least significant bit, abbreviated lsb.

A little review might be helpful to those who are a little rusty on raising a number to a power. No high math here -- to raise a number to a power just write it down the exponent number of times and multiply. The exponent is the power to which a number is raised. One way to recognize an exponent is by the fact that it is often raised when written:
52 = 5 * 5 = 25
23 = 2 * 2 * 2 = 8
44 = 4 * 4 * 4 * 4 = 256

Each bit position has a weight. For all numbering systems I am aware of (the mathematicians probably know of others), the right, least-significant position is known as the 1's place. There, the weight is equal to the base raised to the power of 0. Any number raised to the power of 0 is equal to 1.

The exponent is increased by 1 with each move to the left. Thus, the second place from the right has a weight equal to the base raised to the power of 1. Any number raised to the power of 1 is equal to itself. We were taught in grade school that the second place from the right is the 10's place. That's because we were using a base of 10 and we were raising it to the power of 1. Since a base of 2 is used in binary, the second place from the right has a weight of 2 because it is 2 raised to the power of 1. The next weight is 22 = 4, then 23 = 8 and so on.

The exponents are often used to designate a bit in a binary number. Bit 0 is on the right end of the byte and bit 7 is on the left end. Bit 0 is the lsb and bit 7 is the msb. Data bits are often abbreviated using the letter D -- D0, D1, D2, etc.

Bit D7 D6 D5 D4 D3 D2 D1 D0
Baseexponent 27 26 25 24 23 22 21 20
Weights 128 64 32 16 8 4 2 1


The example binary number above was 01100010. To figure out what the decimal value is, simply add the weights for the bits that are turned on. In this case, bits 6, 5 and 1 are on. The total of their weights equals 64 + 32 + 2 = 98.

A more general description of the procedure is to multiply the position weights by the values at the positions, then add them up. The example 01100010 would be:
(0 * 12 + (1 * 64) + (1 * 32) + (0 * 16) + (0 * + (0 * 4) + (1 * 2) + (0 * 1) = 98.

A common way of representing numbers in a C program is to use hexadecimal notation, or HEX. It uses a base of 16. Break a byte into two groups of 4 bits each: nnnn nnnn. Each group is called a nibble. A nibble with all low bits, 0000, is equal to 0. With all of its bits turned on, 1111, a nibble has a value of 15 (8 + 4 + 2 + 1). Thus, we are dealing with the 16 values from 0 through 15, and a base of 16.

Hexadecimal notation is simple. Just use digits for 0 through 9, and A through F for 10 through 15. The following table shows all of the combinations.

Binary Decimal Hexadecimal Binary Decimal Hexadecimal
0000 00 0 1000 08 8
0001 01 1 1001 09 9
0010 02 2 1010 10 A
0011 03 3 1011 11 B
0100 04 4 1100 12 C
0101 05 5 1101 13 D
0110 06 6 1110 14 E
0111 07 7 1111 15 F


The right nibble of a byte is the least significant nibble. It's the 1's place because it's 160. Next is the 16's place because it's 161, then 162 = 256, and so on. To get the decimal value, take the value of the nibbles, multiply by the position weight values and add them up. Thus, the HEX value 9B = (9 * 16) + (11 * 1) = 155.

To show a number is hexadecimal in the C language, prefix it with 0x. The above would be represented as 0x9B or 0x9b. This particular notation is not case-sensitive, although many things in C are.

The following shows the byte table again, but this time with the weights also expressed in hexadecimal notation, as often seen in C operations.

Bit D7 D6 D5 D4 D3 D2 D1 D0
Baseexponent 27 26 25 24 23 22 21 20
Weights 128 64 32 16 8 4 2 1
HEX Weights 0x80 0x40 0x20 0x10 0x08 0x04 0x02 0x01


A word is usually 16 bits, D0 through D15. A table with the bit names and their relationship to the binary base of 2 is below.

Bit D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
Baseexponent 215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20


The following two tables show the bits with their HEX weights.

Bit D15 D14 D13 D12 D11 D10 D9 D8
HEX Weights 0x8000 0x4000 0x2000 0x1000 0x0800 0x0400 0x0200 0x0100


Bit D7 D6 D5 D4 D3 D2 D1 D0
HEX Weights 0x0080 0x0040 0x0020 0x0010 0x0008 0x0004 0x0002 0x0001


A word can be broken up into 4 nibbles. It can be represented by showing its 4 nibbles as a 4-place hexadecimal number. For example, the decimal number 19070 can be represented as the hexadecimal number 0x4A7E.
0x4A7E = (4 * 163) + (10 * 162) + (7 * 161) + (14 * 160)
= ( 4 * 4096) + (10 * 256) + (7 * 16) + (14 * 1)
= 19070.

In the C language, a word is most often called an integer, abbreviated int. An integer can be used to represent numbers that range from negative to positive values, or numbers that have only positive values. In other words, an integer can be signed or unsigned. A signed integer can have either positive or negative values. An unsigned integer can only be positive. An unsigned 16-bit integer can have values from 0 through 65535. It is often abbreviated simply as unsigned.

Bit 15 is used as a sign bit for signed integers. If it is on, the number is negative. If it is off, it is positive. Positive values can range from 0 to 32767. Negative values can range from -1 to -32768. Some examples are shown below. Notice that the signed version is equal to -1 * (65536 - unsigned version). For example, to get the signed number from the unsigned value 49151,
signed = -1 * (65536 - 49151) = -16385.

HEX 8000 BFFF FFFE FFFF 0000 3FFF 7FFE 7FFF
Signed -32768 -16385 -0002 -0001 00000 16383 32766 32767
Unsigned 32768 49151 65534 65535 00000 16383 32766 32767


A long word is generally considered to be 4 bytes or 32 bits. A long is used for very large numbers. Longs can also be signed or unsigned. Signed longs have a range from -2,147,483,648 to 2,147,483,647. The maximum unsigned value is 0xFFFFFFFF = 4,294,967,295. The minimum unsigned value is 0.

The following is a self-test over this section. It would be a very good idea to make sure you know the answers to all of the questions since the sections that follow will build on this one.

1) Data inside computers is exchanged among the different components by means of metal conductors called __1__. A group is called a __2__.

A) Data Bus, Weight
B) Nibble, HEX
C) Binary, Bit
D) Data Lines, Data Bus

2) If the voltage is 5 volts, the bit is on. If the bit is off, the voltage is 0 volts.

A) True
B) False

3) A group of 8 bits is a __1__ and is called a __2__ in the C programming language.

A) Data Bus, Nibble
B) Byte, Character (or char)
C) Weight, Bit
D) Unsigned, Signed

4) It is said to be a _____ when data is represented with a base of 2, because of the two digits used.

A) Integer (or int)
B) Most Significant Bit
C) Binary
D) HEX

5) The left-end bit can also be referred to as the __1__. The __2__ is the right-end bit.

A) Word, Integer (or int)
B) Character (or char), Data Bus
C) Data Lines, Long Word
D) Most Significant Bit (or msb), Least Significant Bit (or lsb)

6) In the 1's place, the _____ is equal to the base number raised to the power of 0.

A) Byte
B) Weight
C) Long Word
D) Data Bus

7) _____ is a common way of representing numbers in a C program. It uses a base of 16.

A) Data Lines
B) HEX
C) Word
D) Byte

If you break a byte into 2 groups of 4 bits each, then each group is a _____.

A) HEX
B) Binary
C) Nibble
D) Volt

9) A __1__ is usually 16 bits, or 2 bytes. Something with 32 bits, or 4 bytes is usually called a __2__.

A) Signed, Unsigned
B) Byte, Bit
C) Word, Long Word
D) HEX, Binary

10) The __1__ integer can be a positive or negative word, and the __2__ integer can only be a positive word.

A) Nibbles, HEX
B) Byte, Data lines
C) Bit, Integer
D) Signed, Unsigned

este es lo que ay que acer para crear un estadio hexadecimal no malo que esta en ingles pero bueno algo se entendera


:D espero que os sirva

Sab Abr 08, 2006 1:17 am

Asi se supone que se editan los estadios???
Esos tipos deben ser unos maestros.

Sab Abr 08, 2006 2:05 am

vaya tio!!,si alguien puede traducir esto, please??? pq encima q es dificil la edicion hexadecimal,lo de la explicacion en ingles raya un poco...jeje

Full Version