CogSci
3
Introduction to Computing

Home

Syllabus

Schedule

Contact Us
Regrade Form

Printable
Version

Department of
Cognitive
Science

Binary Numbers

(Uses JavaScript and requires Netscape 3.0 or Internet Explorer 4.0 or higher)

Number Systems

The numbers we normally use are based on the decimal number system. Starting at the right and working to the left, each digit in a decimal number represents a larger power of 10. That is, the right most digit is 100; 10 raised to the zero power. (By the way, in case you didn't know, any number raised to the zero power is just 1.) The digit to the left of the "ones" column is the 101 digit, or "tens". To the left of the "tens" is the 102 digit (10 x 10), or "hundreds".

To get the total value of a number, say: 1,234, you take each digit and multiply it by the appropriate power of 10. Then add the all products up to get the final value. This procedure may be so ingrained, that it seems silly to even talk about. Maybe the following table will help illustrate the details.

Example Decimal Number: 1,234

Power of 10 103 102 101 100
Value of Power 1000 100 10 1
Digit value 1000 200 30 4
1000+200+30+4=1,234

Okay, so what? Well, there are number systems based on values other than 10!

Binary Numbers

Internally computers use binary numbers. The binary number system is based on 2 (or powers of 2) rather than the normal decimal numbers that we are used to. Each digit in a binary number is either a 0 or 1, and is stored as a single "bit" in the computer. Rather than 102101100 type values for each digit column, binary numbers have 222120.

We don't usually worry about binary, because when the computer prints out a number for us, it has been translated into decimal. Similarly, when we input a decimal number into the computer (say, into a spreadsheet), it is translated into binary. The table below shows some binary numbers and their decimal equivalents.

2N Binary Number Decimal Value
20 1 1
21 10 2
22 100 4
23 1000 8
24 10000 16
25 100000 32
26 1000000 64
27 10000000 128
28 100000000 256
29 1000000000 512
210 10000000000 1024
... ... ...
216 10000000000000000 65,536
... ... ...
220 100000000000000000000 1,048,576
... ... ...
224 1000000000000000000000000 16,777,216

As you can see, binary numbers can be very long (many digits) and cumbersome to read and write. For instance, 10000001 is the same as 129 in decimal! However, when programming a computer, it is sometimes convenient to work directly with binary and to specify the value that each bit in a binary number will have. Enter hexadecimal.

Hexadecimal

Hexadecimal is a number system based on 16, which is a power of 2 (24). Hexadcimal, or hex, is a more compact way to express a binary number. Each hexadecimal digit represents 4 bits, or a binary number between 0 and 15 (decimal). The first 10 digits are written 0-9 just as in normal decimal. The rest of the digits (which represent values from 10-15) are written with the letters A-F, with A equal 10 decimal and F equal 15.

Here is our example of 1,234 again, only as a hexadecimal number. In hex, its just 1,234. But the table translates each digit into a decimal equivalent.

Example Hexadecimal Number: 1,234

Power of 16 163 162 161 160
Value of Power
(Decimal)
4096 256 16 1
Digit value
(Decimal)
4096 512 48 4
4096+512+48+4=4,660

Since each hexadecimal digit represents (exactly) 4 binary digits (or bits), the numbers are much shorter than the binary equivalent. Eight bits make up a byte--2 hexadecimal digits. Each hex digit in a byte is also called a nybble. Each byte (2 hex digits) is a hexadecimal number between 0 and FF (which is 255 decimal).

A Byte is a Byte

Okay, so in summary:
  • We have binary numbers which can also be written more compactly as hexadecimal.
  • Though we didn't explain how, we have said the computer translates its internal binary numbers to and from decimal when doing output and input.
  • Eight bits make up a byte.
  • A byte can have hexadecimal values between 0 and FF, or 0 and 255 decimal.

It's a tad more complicated than that. In most modern computers, like PCs and Macintoshes, the exact interpretation of the value in a particular byte in the RAM is up to the software program that is looking at it.

Say a particular byte contains the binary number 1000001, which is 41 in hexadecimal, or 65 in decimal. That bit pattern can also represent the ASCII (American Standard Code for Information Interchange) code for the letter "A"; i.e., the printable letter capital "A". (The code sent to a printer to cause the letter "A" to appear on the page.) That byte might also be part of a computer instruction, (also called machine language) like Add, or Compare.

Or that byte might be part of a longer sequence of bytes that comprise a 16 bit, 32 bit, or 64 bit number! Perhaps that byte is part of the RGB (Red Green Blue) color specification of a Web page background color or font color. Or part of a black and white image in which each single bit specifies if a pixel (picture element) is black or white.

The interpretation of that byte is determined by the software programmer who wrote the software which is looking at it.

Let's Get Negative

Back to number systems. So far we have only talked about positive binary numbers. We also need to have negative numbers to hold values like that of your bank balance after you've written too many checks. What do negative numbers look like?

In our human decimal system, we just prefix a negative number with a minus sign ('-'). But computers only have bits to represent things. It is possible to designate a sign bit to say whether the value was positive or negative. This turns out to be wasteful in terms of storage, that sign bit can't also be used as a numeric value. Instead, most (not all!) modern computers two's compliment notation for negative numbers.

In two's compliment, the highest order (most significant-- leftmost) bit determines whether the value is positive or negative. If the bit is "on", a 1, then the number is negative. Otherwise, it is positive.

So our lowly byte, which we had been viewing as unsigned (positively only) had values been 0 and 255 (decimal). As a signed value, it can have values between -128 and 127. Which interpretation, signed or unsigned, of the bit pattern is up to the software looking at it.

The Binary Workbench

In the tabular form below, there is a 16 bit number composed of two 8 bit bytes. By clicking on the radio button toggles, you can change the value of a particular bit from a 0 to a 1 and then back to 0 again. (A toggle is a switch that selects between one of two values.) Only the toggles are active for input, by the way.

The Format pop up menu lets you choose your interpretation of the 16 bits: unsigned decimal, signed decimal, ASCII, etc. When you select a new output format, the output will be adjusted accordingly.

Some things to try:

  • Both a signed and unsigned number with only the most significant (2d15 bit set (=1).
  • What is the ASCII equivalent of 0100001101100011?
  • What is the two's complement (binary) representation of -1?

A 16 Bit Number

Byte 2

Byte 1

Nybble 4 Nybble 3 Nybble 2 Nybble 1

Bit Value
Binary
Toggle
215 214 213 212
211 210 29 28
27 26 25 24
23 22 21 20

Output Format


Output



* Until recently, ASCII was a 7 bit code, values between 0 and 127. When building the ASCII output formatter, I couldn't decide whether to just interpret the low order 7 bits or show the new values above 128 too. I decided to try the latter.

I include quotes ('"') around the ASCII codes to remind you that they are a character string. So, "CA" are the printable characters C and A, which are different than the hexadecimal value CA. The quotes also let the space character "show up".

Finally, control characters which have values between 0 and 31 typically do not have a printable form. I translate those as a carat ('^') followed by the letter you would type with the control key to input that value. E.g., hexadecimal 01 will show up as "^A".


Terms to Know

ASCII computer instruction nybble twos compliment
binary control characters RGB unsigned
bit decimal sign bit
byte Hexadecimal signed value
character string machine language toggles

WML
©opyright 1997-2005 Mark R. Wallen
Last updated: Mon Aug 8 16:31:53 2005