Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Outline of Class 29: Binary Representation

Miscellaneous

• Like many of you, I'm a procrasinator. In this case, it means that I haven't finished reviewing your midterms (that they got stuck in the car during the snowstorm didn't help). I will do my best to get them back to you on Wednesday.
• I'll try to get the outstanding homework assignments back by Friday, but that's less likely.
• Thanks for participating in the web survey. I've made a few changes to the course web and will try to do more (within time constraints). If you're interested in what I did with it, see the paper I wrote over break. Some of the results may appear odd. I just realized (early this morning) that there were some problems with the survey software, but those problems will be fixed in the final version of the paper.
• There is a new assignment.
• While we discussed some issues in binary representation before break, I'll make few assumptions that you remember that discussion.
• There's an old student-authored handout on binary representation available.

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.
• All of computing can be done with intelligent use of bits and circuitry.

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.
• Typically, a byte is eight bits
• Typically, a word is some multiple of bytes, 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.
• Observe that there are (at least) three versions of each number:
• The pure, mathematical entity.
• The representation in ASCII text (or which there may be many)
• The representation in binary (of which there is at most one)

Integers

• It is easy to represent positive integers as standard binary numbers. The rightmost bit is the 1's column (1=2^0), the next is the 2's column (2=2^1), the next is the 4's column(4=2^2), 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?

Outlines: prev next

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

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.