Sunteți pe pagina 1din 10

Number Systems A number system is a systematic way to represent numbers with symbolic characters and uses a base value

to conveniently group numbers in compact form. The most common number system is decimal, which has a base value of 10, and a symbolic character set of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. However, there are other number systems, and they can be more efficient to use for a specific purpose. For example, because computers use Boolean logic to perform calculations and operations, they use the binary number system, which has a base value of 2. There are infinite ways to represent a number. The four commonly associated with modern computers and digital electronics are: decimal, binary, octal, and hexadecimal. Decimal (base 10) is the way most human beings represent numbers. Decimal is sometimes abbreviated as dec. Decimal counting goes: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, and so on. Binary (base 2) is the natural way most digital circuits represent and manipulate numbers. (Common misspellings are bianary, bienary, or binery.) Binary numbers are sometimes represented by preceding the value with '0b', as in 0b1011. Binary is sometimes abbreviated as bin. Binary counting goes: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, and so on. Octal (base 8) was previously a popular choice for representing digital circuit numbers in a form that is more compact than binary. Octal is sometimes abbreviated as oct. Octal counting goes: 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, and so on. Hexadecimal (base 16) is currently the most popular choice for representing digital circuit numbers in a form that is more compact than binary. (Common misspellings are hexdecimal, hexidecimal, hexedecimal, or hexodecimal.) Hexadecimal numbers are sometimes represented by preceding the value with '0x', as in 0x1B84. Hexadecimal is sometimes abbreviated as hex. Hexadecimal counting goes: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, and so on. All four number systems are equally capable of representing any number. Furthermore, a number can be perfectly converted between the various number systems without any loss of numeric value. At first blush, it seems like using any number system other than human-centric decimal is complicated and unnecessary. However, since the job of electrical and software engineers is to work with digital circuits, engineers require number systems that can best transfer information between the human world and the digital circuit world. It turns out that the way in which a number is represented can make it easier for the engineer to perceive the meaning of the number as it applies to a digital circuit. In other words, the appropriate number system can actually make things less complicated.

Numeral Systems Conversion : Binary, Octal, Decimal and Hexadecimal As a computer programmer, you need to know about the different numeral systems. In your computer works, there will be lots of times that you will be using numeral systems like the binary, hexadecimal and sometimes octal as well. Thus, it would be a great idea to know how to convert numbers from one numeral system to the other. The digits that all 4 numeral systems use are shown below : Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Binary 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20

Convert From Decimal to Other Numeral Systems To convert a number from decimal to any other numeral system, we follow a standard procedure. We divide the integer part of a number with the base number of the system at which we want the actual conversion to occur. For instance, to convert a number from decimal to binary(base 2), we divide that number with 2. While we make each division, the remainder of the division is the rightmost digit of the resulting number in binary and the result of the division gets redivided with 2 till we reach the division where the result is 0. If the number has a fractional part, this gets multiplied by two. The integer part of the number after the multiplication is held, will be the leftmost digit of our new fractional number in the binary system, right after the comma. The result of the

multiplication of 2 with the fractional part of the number gets substracted of the integer part used for the creation of the fractional number in the binary system. This new number gets multiplied with 2 again and then the previous procedure is executed again and again, till the new fractional number becomes 0. If the new fractional number is periodic, we cut and round the resulting number. This may sound a bit confusing, so these are some example conversions : Convert from decimal to binary (10)->(2) Integer 45(10)->(2) Div 45 / 2 22 / 2 11 / 2 5/2 2/2 1/2 Quotient 22 11 5 2 1 0 1 0 1 1 0 1 Remainder 1 01 101 1101 01101 101101 Binary Number ()

45(10)->101101(2) Fractional Part 0,182(10)->(2) Div 0,182 * 2 0,364 * 2 0,728 * 2 0,456 * 2 0,912 * 2 0,824 * 2 0,648 * 2 Product 0,364 0,728 1,456 0,912 1,824 1,648 1,296 Remainder 0 0 1 0 1 1 1 Binary Number () 0,0 0,00 0,001 0,0010 0,00101 0,001011 0,0010111

0,182(10)->0,0010111(2) (After we round and cut the number) Convert from decimal to octal (10)->(8)

Integer 45(10)->X(8) Div 45 / 8 5/8 Quotient 5 0 5 5 Remainder 5 55 Octal Number ()

45(10)->55(8) Fractional Part 0,182(10)->(8) Mul 0,182 * 8 0,456 * 8 0,648 * 8 0,184 * 8 0,472 * 8 0,776 * 8 Product 1,456 3,648 5,184 1,472 3,776 6,208 1 3 5 1 3 6 Integer 0,1 0,13 0,135 0,1351 0,13513 0,135136 Binary Number ()

0,182(10)->0,135136(8) (After we round and cut the number) Convert from decimal to hexadecimal (10)->(16) Integer 45(10)->X(16) Div 45 / 16 2 / 16 Quotient 2 0 13 2 Remainder Hex Number () D (Since 13 decimal is D in hexadecimal) 2D (See the table)

45(10)->2D(16) Fractional Number 0,182(10)->(16) Mul Product Integer Binary Number ()

0,182 * 16 0,912 * 16 0,592 * 16 0,472 * 16 0,552 * 16 0,832 * 16

2,912 14,592 9,472 7,552 8,832 13,312 9 7 8

2 14

0,2 0,2 0,29 0,297 0,2978 0,2978D

13

0,182(10)->0,2E978D(16) (After we round and cut the number) 2. Convert from other numeral systems to decimal Now, in order to do the opposite, we have to do some pretty different steps than the previous ones. At first, we count the number of digits that our number to convert consists of starting from 0 and going from right to left when the number is an integer. However, when the number is decimal, then we count the digits of the number right after the comma, starting from left and going to the right, indexing them starting from -1. So, in order to convert a number from one numeral system to decimal, we multiply each digit with a number that has its base on the numeral system that the number is represented at. For instance, for binary that number is 2 and we raise that number to the index that each digit is at. In the end, we add all the resulting numbers and the result is that number in decimal. Take a look at the examples below to understand it better : Convert from binary to decimal (2)->(10) 101101,0010111(2)->(10) Index the digits of the number 150413120110,0-10-21-30-41-51-61-7 Multiply each digit 1 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 + 0 * 2-1 + 0 * 2-2 + 1 * 2-3 + 0 * 2-4 + 1 * 2-5 + 1 * 2-6 + 1 * 2-7 = (- = 1/) 32 + 0 + 8 + 4 + 0 + 1 + 0 + 0 + 0,125 + 0 + 0,03125 + 0,015625 + 0,007813 = 45,179688(10) Convert from octal to decimal (8)->(10) 55,135136(8)->(10) Index the digits of the number 5150,1-13-25-31-43-56-6 We multiply each digit

5 * 81 + 5 * 80 + 1 * 8-1 + 3 * 8-2 + 5 * 8-3 + 1 * 8-4 + 3 * 8-5 + 6 * 8-6 = 40 + 5 + 0,125 + 0,03125 + 0,009766 + 0,000244 + 0,0001 + 0,0000229 = 45,1663829(10) Convert from hexadecimal to decimal (16)->(10) 2D,2E978D(16)->(10) Index the digits of the number 21130,2-114-29-37-48-513-6 We multiply each digit 2 * 161 + 13 * 160 + 2 * 16-1 + 14 * 16-2 + 9 * 16-3 + 7 * 16-4 + 8 * 16-5 + 13 * 16-6 = 32 + 13 + 0,125 + 0,0546875 + 0,00219727 + 0,00010681 + 0,00000762 + 0,00000077 = 45,18199997(10) Notice that the decimal numbers are not actually the same as their primary values. We have some losses. These losses are due to the rounds and cuts done in the previous results. 3. Convert from binary to hexadecimal to octal As we all now, 23 results to 8 and 24 gives 16. This reality, believe it or not, will help you understand how to convert from binary to octal and hexadecimal and vice versa. As we said before, 23 gives 8, so in order to convert a binary number to octal, we need 3 digits that will represent a digit of the octal number. Therefore, for a hexadecimal number we need 4 digits to represent a hexadecimal digit. For that reason, we convert a binary number to octal, then separate the binary number into triads or quadruples (when hex), starting from right to left when talking about integers and from left to right when its about decimals starting from the comma. If there are digits left in the end that do not make a triple or a quadruple, for the hex system we pad with 0 in front of the number. Then, we substitute each three digit number or four digit binary number with a digit that corresponds to octal when its triples or hex when its quadruples. In this conversion, you will be helped from the table above. Here are some examples to understand what was said : Convert from binary to octal 110101000,101010(2)->X(8) |3| |3||3| |3| |3|

110 101 000 ,101 010 || \/ 6 || \/ 5 || \/ 0 || \/ , 5 || \/ 2 (See that in the array 110(2) corresponds to 6(8) )

110101000,101010(2)->650,52(8) Convert from binary to hexadecimal

110101000,101010(2)->X(16) |4 | | 4| | 4| | 4| | 4| 0001 1010 1000 ,1010 1000 || \/ 1 || \/ || \/ 8 , || \/ || \/ 8

110101000(2)->18,8(16) 4. Convert from hexadecimal to octal and binary This is the exact reverse procedure than the previous one. As previously, each digit of the octal number corresponds to 3 digits of the binary number and 4 digits of the hexadecimal number. When the appropriate triples or quadruples are not completed, we pad with 0 in front of each number : Convert from octal to binary 650,52(8)->X(2) 6 || \/ || \/ 5 || \/ 0 , 5 2

|| || \/ \/

110 101 000 , 110 010 650,52(8)->110101000,110010(2) Convert from hexadecimal to binary 18,8(16)->X(2) 1 || \/ || \/ || \/ 8 , || \/ || \/ 8

0001 1010 1000 ,1010 1000

Conversion of Numbers The following offers some rules for conversion of numbers between differing number systems. You may find other approaches that are more suitable (and understandable) e.g. the tabular layout outlined on the previous three pages. Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17

Examples 1. Binary to Octal Conversion : Binary Number 1001001011110101 Split into groups of 3 binary digits (bits) starting from the right. 1 001 001 011 110 101 Binary 1 1 1 3 6 5 Octal 2. Binary to Hexadecimal Conversion : Binary Number 1001001011110101 Split into groups of 4 bits starting from the right. 1001 0010 1111 0101 Binary 9 2 F 5 Hexadecimal 3. Decimal to Binary: Example: Convert 46(base 10) to binary. Divide the number successively by 2 and note the remainders (0 or 1). Write down these remainders, starting from the right and working left. 46 / 2 rem 0 0 23 / 2 rem 1 10 11 / 2 rem 1 110 5 / 2 rem 1 1110 2 / 2 rem 0 01110 1 / 2 rem 1 101110 0 Binary 0101110 Binary Addition

0 +0 0

0 +1 1

1 +0 1

1 +1 carry(1)0

If a carry is generated, it is carried one position to the left as in decimal arithmetic. Example: Add binary numbers 101 and 111 101 +111 1100 Starting from the rightmost column, 1 + 1 means that you write 0 and carry 1 to the next column on the left. Move to the left, add 0 + 1 plus the 1 you carried. Therefore you write 0 and carry 1 to the next column. Move to the left, add 1 + 1 plus the 1 that you carried. Therefore you write 1 and carry 1 to the next column. Move to the left, nothing to add other than the 1 you carried. Therefore the 1 is placed in front of the other 3 binary digits. Result 1100 Number System Exercises Left click on the A icon to get the answers (switch off pop-up blocker if you have one enabled). 1. Convert the following IP addresses into 32-bit binary form. (i) 127.0.0.1 (ii) 199.34.57.255 (i) 01111111 00000000 00000000 00000001 (ii) 11000111 00100010 00111001 11111111 2. Convert the following decimal RGB values into hex form. (i) R 255 G 000 B 051 (ii) R 102 G 204 B 153 (i) FF0033 (ii) 66CC99 3. Add the following binary numbers (i) 00010110 + 00111111 (ii) 01111101 + 01100011

(i) 01010101 (ii) 11100000 4. Convert the answers to 3(i) and (ii) into decimal numbers. (i) 85 (ii) 224 5. By using the 8-bit ASCII table, the following binary code can be used to represent the four characters M150 01001101 00110001 00110101 00110000 Give the decimal representation of each character. 77 represents character M 49 represents character 1 53 represents character 5 48 represents character 0

S-ar putea să vă placă și