# Class 33: Introduction to Machine Representation

Back to Animation. On to More Binary Representation.

Held Monday, April 5

Summary

• Fundamental types
• Nonnegative integers

Contents

Handouts

Notes

## Binary Representation

• It turns that that we're very good at building electronic devices that can represent pairs of values. We call such devices binary devices and generally refer to the values they represent as 0 and 1. Each value is called a bit.
• In fact, all of computing can be done with intelligent use of bits and circuitry.
• Most electronic circuits are approximate, sometimes passing less than full current when ``on'', sometimes passing current even when ``off''. Hence, 1 is typically ``passes enough current that it seems to be on'' and 0 is ``passes so little current that it seems to be off''.

### Representing Fundamental Types

• We normally speak of primitive types like integers, floats, and such.
• These types are built into the language.
• But they need to be implemented at some level (in compiler, virtual machine, or hardware).
• In most machines, values are stored as fixed-length sequences of bits (0/1 values).
• Bits are grouped into bytes and words.
• A byte is typically eight bits.
• A word is typically some multiple of the byte size, usually two or four bytes.
• The bits in a group are numbered from right to left, starting with 0.
• The rightmost bit in an eight-bit byte is the 0th bit.
• The leftmost bit in an eight-bit byte is the 7th bit.
• There are many ways to think about representation. For example, you might observe that there are (at least) three versions of each number:
• The pure, mathematical entity.
• The representation in ASCII or Unicode text (or which there may be many).
• The representation in binary (of which there is at most one).

### Integers

• It is easy to represent nonnegative integers as standard binary numbers. The rightmost bit is the 1's column (1=20 ), the next is the 2's column (2=21), the next is the 4's column(4=22), and so on and so forth.
• Addition and multiplication are stratighforward for this representation (and surprisingly easy to represent in circuitry).
• However, if we limit the number of bits per integer, we have to allow for overflow when more bits are needed to represent the results than the operands.
• But what happens when we need to represent negative integers? What makes a representation scheme good?
• Only one representation for each number (or at most two for some numbers).
• Represents a wide range of values.
• Easy to use in computation (preferably, using the aforementioned routines or simple variants).
• What are strategies for representing negative integers? They include signed magnitude, one's complement, two's complement, and excess 2(m-1).
• Signed magnitude. Use the leftmost bit to indicate the sign.
• Positive numbers begin with 0, negative numbers begin with 1.
• There are two representations of 0 (+0 and -0).
• With eight bits, you can represent everything between -127 and +127.
• Negation is easy.
• But addition and subtraction are hard.
• One's complement. To negate a number, flip the bits (0 to 1, 1 to 0).
• Positive numbers begin with 0, negative numbers begin with 1.
• There are two representations of 0 (00000000 and 11111111 in eight-bit).
• With eight bits, you can represent everything between -127 and +127.
• Negation is easy, but slightly time consuming.
• It's not clear that addition and subtraction are any easier.
• Two's complement. To negate a number, flip the bits and then add 1, using standard addition. Throw away any overflow bits.
• Positive numbers begin with 0. Negative numbers begin with a 1.
• There is only one representation of 0 (00000000 to 11111111 add 1, giving 100000000, but the 1 is an overflow).
• Negation is easy, but time-consuming (n flips, up to n steps in the addition).
• Surprisingly, the standard binary addition routine works.
• Excess 2(m-1). To represent x, instead represent x+2(m-1) using the positive-only representation (where m is the number of bits in the representation).
• This is sometimes called a biased representation.
• Positive numbers begin with 1. Negative numbers begin with 0.
• There is only one representation of 0, 10000000.
• The largest number we can represent is 11111111 = 127.
• The smallest number we can represent is 00000000 = -128
• How do you do negation?
• How do you do addition?

## Representing Different Ranges

• One disadvantage of the three standard representations of signed integers (signed magnitude, one's complement, two's complement) is that all three support only a fixed range of values.
• In a biased representation of a range of integers, you select a bias (offset) and then (traditionally) use the standard positive-only representation.
• If the bias is b, you represent n using the positive-only representation of b+n.
• To represent the numbers from -1 to 254 in one byte, you use a bias of 1.
• To represent the numbers from -255 to 0 in one byte, you use a bias of 255.
• Alternately, you can think of a biased representation as taking the series of bits, computing the corresponding positive integer, and then subtracting the bias to determine the actual value represented.
• The most typical bias is 2(m-1), where m is the number of bits used to represent the number. This is called excess 2(m-1).
• For one byte, the bias is 128. This means that the smallest number we can represent is -128 and the largest is 127.

History

• Created Monday, January 11, 1999.
• Added short summary on Friday, January 22, 1999.
• Filled in the details on Tuesday, March 29, 1999. Most were based on outline 34 of CS152 98S.
• Added a few more notes on Monday, April 5, 1999.

Back to Animation. On to More Binary Representation.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.