Number Systems for Computer Programmers.-VBForums

# Thread: Number Systems for Computer Programmers.

1. ## Number Systems for Computer Programmers.

Number Systems for
Computer Programmers.

As a computer programmer, you might have to work with different types of number systems. Starting from well known Decimal Number Systems to Binary and Hexadecimal are the most usable number systems in programming.

In this tutorial I’m going to explain what these systems are and how they interact with each other.

Contents:

1. Basic Rules and Definitions
2. The Decimal Number System
3. The Binary Number System
• Two’s-Complement Notation
5. Conversion of One Number System to Another
• From Decimal to Binary
• From Binary to Hexadecimal and Vise Versa
6. Decimal Numbers and their Binary and Hexadecimal Equivalents

2. ## Basic Rules and Definitions

Before go further I want to point out few basic rules in mathematics, which I’m going to use later in my tutorial.

1. Any number to the 0th of power equals to 1.
2. Any number to the 1st of power equals to the same number (number itself).
3. Any number can be represented as powers of the system’s base.
4. The system of base n requires n number of digits.

Following definitions are used;

1. Digits of a number is count from right to left and the right most digit is the first digit of that number.
2. First digit holds 1st place value, second digit holds 10th place value, third digit holds 100th place value, and so on.
3. Most Significant Bit (MSB) is the left most bit of a binary value.
4. Least Significant Bit (LSB) is the right most bit of a binary value.

3. ## The Decimal Number System

The decimal number system is base-10 system and it is the most common number system that you use everyday. It requires 10 different digits, from 0 to 9.

For an example, 154-is expressed as powers of 10. The digit 4 (first digit) gives 10 to the 0 power, digit 5 (second digit) gives 10 to the 1 power, and so on.

Number 154 can be deploying as follows.

Code:
```
1	1 x 102 = 1 x 100 =  100
5	5 x 101 = 5 x 10  =   50
4	4 x 100 = 4 x 1   =    4
Sum   = 154 (Decimal)```

Mathematical representation of 154 in base-10 is, 15410. But it can be written as 154 without any uncertainty.

4. ## The Binary Number System

The binary number system is base-2. Hence it requires only two digits, 0 and 1. This binary number system is very useful for computer programmers, because it has two stages and can be used to represent the digital on/off status in which computer chips and memory work.

Here’s an example for a binary number, 1101. It is “One-One-Zero-One”, is the way that a binary number is pronouncing, digit-by-digit from left-to-right, as usual.

Its representation in the decimal format, which you’re more familiar with is as follows.

Code:
```
1	1 x 23 = 1 x 8 = 8
1	1 x 22 = 1 x 4 = 4
0	0 x 21 = 0 x 2 = 0
1	1 x 20 = 1 x 1 = 1
Sum = 13 (decimal)```
Once again, standard representation of a binary number is, 11012.

Two’s-Complement Notation

Two’s-complement notation is an efficient way of representing signed numbers in computer chips and memory. When a number is written in two’s-complement notation, the most significant bit of the number represent its sign; 0 means that the number is positive, 1 means the number is negative.

A positive number can be written in two’s-complement notation is the same as the number written in unsigned notation, and although the most significant bit must be zero.

A negative number can be written in two’s-complement notation by inverting all of the bits of its absolute value, and then adding one to the result.

Example:

Code:
```
1 = 00012 	-1 = 11102 + 12   = 11112

2 = 00102	-2 = 11012 + 12   = 11102

8 = 10002	-8 = 01112 + 12   = 10002

9 = 01012	-9 = 10102 + 12   = 10112```
Emphasize that, 10002 represents -8, not 8. That is because the most significant bit is 1, indicating that the number is negative.

5. ## The Hexadecimal Number System

The hexadecimal system (also called as hex system) is base-16, so that it requires 16 different digits. The digits from 0 to 9 are used, along with letters from A to F, which represent the decimal values from 10 to 15 respectively.

1AF is an example for hexadecimal number. Its decimal format;

Code:
```
1	1 x 162  =  1 x 256 = 256

A	A x 161  = 10 x  16 = 160

F	F x 160  = 15 x   1 =  15

Sum          = 431 (decimal)```
Standard representation of the above hexadecimal number is 1AF H; put the letter ‘H’ at the end.

The hexadecimal system is also useful in computer works, because it’s based on power of 2.

Each digit in the hexadecimal system is equivalent to four digits in the binary system, two digits in the hexadecimal system is equivalent to eight digits (1 byte) in the binary system, and so on.

Example:

Binary value 111110102 holds two hexadecimal values. One is by 10102 and the other one by 11112. In hexadecimal they represent A H and F H respectively.

So, 111110102 = FA H

6. ## Conversion of One Number System to Another

Now you already know that how to convert binary and hexadecimal values into decimal values. So I don’t want to discuses it again and make my tutorial so bulky and unpleasant to you.

First I’ll explain that how to convert a decimal value into binary and hexadecimal value. And then explain that how to convert a binary value into hexadecimal and vise versa.

From Decimal to Binary

Divide the given decimal number by 2, until can’t do further dividing. And at the same time write-down the remainder values respectively. First remainder value is the least significant bit (LSB) and the last remainder value is the most significant bit (MSB) of the derived binary number, from the given decimal number.

Divide the given decimal number by 16, until can’t do further dividing. Then follow the same procedure used to derive the binary number from decimal number.

From Binary to Hexadecimal and Vise Versa

To convert binary number into hexadecimal number, group the binary number into four bits segments. If there is not enough bits to do that it is allow you to add zeros before the most significant bit. Then find the appropriate hexadecimal number through the decimal number of the each four bits segment. It’s not difficult. Just you have to keep in mind 00002 to 11112 binary values, along with respective hexadecimal values from 0 H to F H. Those values are included in table “Decimal Numbers and their Binary and Hexadecimal Equivalents”.

Example:

Code:
```
111110102	=	(1111	1010)2		=	FA H

100111012	=	(1001	1101)2		=	9D H

1010112	=	 (0010	1011)2		=	2B H
```
If you want to convert a hexadecimal number into binary number, just find the relative binary number through decimal numbers for each single digits of hexadecimal number.

7. ## Decimal Numbers and their Binary and Hexadecimal Equivalents

The following table state that the decimal numbers with there equivalent binary and hexadecimal values.

Code:
```
Decimal Number 		       Binary Equivalent	        Hexadecimal Equivalent

0				0000				0
1				0001				1
2				0010				2
3				0011				3
4				0100				4
5				0101				5
6				0110				6
7				0111				7
8				1000				8
9				1001				9
10				1010				A
11				1011				B
12				1100				C
13				1101				D
14				1110				E
15				1111				F
16			    	00010000			10
17				00010001			11
50				00110010			32
240				11110000			F0
250				11111010			FA
255				11111111			FF

```
That's all about the number systems. Hope you gain a tiny. If you think that there should any changes, or suggestions please feel free to send it to me.

Thanks,

Best regard,
Eranga.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured