We're looking for your comments on how to best organize the wiki's content.

Decimal, Binary, and Hex

From 0x10c Wiki
Revision as of 15:59, 21 April 2012 by 64.121.91.97 (Talk)
Jump to: navigation, search

When you think of numbers, you probably think of decimal numbers, since they are one of the most popular ways of representing numerical values today. This tutorial will teach you how to convert values represented in decimal to binary and hexadecimal, two of the most useful representations for Assembly Programming.

Contents

Converting Other Bases to Decimal

"931" is a three-digit decimal number, with each digit represented by an Arabic numeral from 0 to 9. Digits on the left have a greater value than digits on the right. In grade school, we were told that the "1" is said to be in the "one's place", the "3" in the "ten's place", and the "9" in the "hundred's place". However, we will simply number the positions from right to left, starting with zero. So for the number "931", the "1" is in position 0, the "3" in position 1, and the "9" in position 2.

To calculate the value of "931", you simply take the sum of all the digits multiplied by 10their position.

1 x 100 + 3 x 101 + 9 x 102 = 1 + 30 + 900 = 931

This seems fairly self-explanatory, but the trick is that this approach works for any type of integer. You've probably heard of decimal numbers also referred to as "base-10". The "10" in "base-10" is referred to as the radix, which is the number of values that each digit can hold - in decimal, that would be the values 0 through 9.

Numbers are usually represented in computers as binary, or "base-2". This means that each digit can either be a 0 or a 1. In order to determine the value of a binary number, such as 11002, you follow the same procedure as above.

0 x 20 + 0 x 21 + 1 x 22 + 1 x 23 = 0 + 0 + 4 + 8 = 12

Another useful notation is hexadecimal, which is "base-16". Since we don't have Arabic numerals to represent the values 10 through 15, we get creative and borrow from another alphabet. So the 16 values for hex representations are the Arabic numerals "0-9" and the latin characters "a-f". So "12" is represented in hex by a "c". To take a number in hex and convert it to decimal, we follow the same formula. Let's convert 8b416 into decimal.

4 x 160 + 11 x 161 + 8 x 162 = 4 + 176 + 2048 = 2228


Converting Decimal to Other Bases

While going from an arbitrary base to decimal is pretty easy, the opposite is a little trickier. Basically, you start with the value you want to represent and divide it by the radix. The remainder is stored in digit 0. Divide the result of the previous division by the radix again, and store the remainder in digit 1. Keep following these steps until the resut of division is 0, and the remainders will show the representation of the value in the new radix.

Let's give it a shot and write 10 in binary.

10/2 = 5 remainder 0,
5/2 = 2 remainder 1,
2/2 = 1 remainder 0,
1/2 = 0 remainder 1, 10 = 10102

We can do the same thing with hex. Let's do it with 1000.

1000/16 = 62 remainder 8
62/16 = 3 remainder 14
3/16 = 0 remainder 3, 1000 = 3e816

Converting Between Hex and Binary

Large strings of numbers like 1100011011100011010111012 are very difficult to read as the digits all start to blend together. In many countries, seperators like commas or periods are put between each three digits in a decimal number. In assembly, we often break the string into blocks of four digits, seperated by spaces. So the number above becomes 1100 0110 1110 0011 0101 11012.

Splitting the numbers at each four digits is not an arbitrary decision - as you can tell, each block of four binary digits corresponds to a value between 0 and 15 which is, oddly enough, the range of hexadecimal values! After becoming quick at converting from binary to decimal, you can quickly translate each block into a single hex digit, making it possible to quickly rewrite the above string directly into c6e35d16. You can also convert a single hex digit back into a four-digit binary string, making it much more efficient to convert from hex to binary than using decimal.

Notation

Typically, to represent a number in a base other than decimal, we put a subscript with the radix after the number, like in 11002. However, it's hard to represent subscripts in machines, so we have special prefixes instead. Hex values are often prefixed by 0x or $, while binary values are often prefixed by 0b or %. So 11002 would be represented as 0b1100, making it stand out from 1100 or 0x1100.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox