Computer Science 161.01

Professor Valente

 

Friday August 30, 2019 

 

TEXTBOOK READING: Sections 3.1, 3.3, 3.5

 

 

Data Types

 

In section 3.1, we are reminded that computers process various types of data: textual, numeric, audio and video are a few that come to mind. 

We very quickly learn that in order for each of these types to actually be processed, they must be represented in binary, that is, by 0’s and 1’s.

 

In this chapter, you and I will focus on how to represent text and how to represent color. 

We will see that representing any reasonable amount of text, for example, requires many bits.

Thus we will study a shorthand way of representing binary, known as hexadecimal (or hex for short) and a related way known as octal.

 

Let’s begin by understanding how much information can be represented by a particular number of bits.

With 1 bit, my only 2 possibilities are 0 and 1. 

With that, I could represent the answer to a yes-or-no question, the result of a coin flip, whether a light switch is on-or-off, etc.

 

What about 2 bits?  (Think of having 2 light switches.  How many different ways can they be set?).

If you said 4 possibilities, you are catching on! 

The 4 binary patterns are 00,01,10,11.

 

How many 3-bit patterns are there?   What are they?   See if you can generate them from the 2-bit patterns.

How many 4-bit patterns are there?   What are they?   See if you can generate them from the 3-bit patterns.

(OK, that pretty much exhausts the possibilities for light switches in our classroom…)

 

Here’s a question:  What happens to the number of patterns every time you allow another bit (or another light switch)?

Yup, it doubles!  

 

Putting this altogether, you’ll realize that for any positive integer n, there are 2n bit-patterns.

So, for example, 7 bits allow for 128 patterns. 

Think about whether 7 bits will allow us to represent all the text symbols we’d like (uppercase and lowercase letters, etc).

 

Finally, as your textbook points out, an 8-bit pattern is known as a byte.  How many different bytes are there?

 

 

Representing Text using the ASCII system

 

The American standard for assigning each keyboard symbol a 7-bit pattern is known as the ASCII code.

For example, A is represented by 1000001, B is 1000010, and C is 1000011.  See if you can predict what D is!

Interestingly, lower case letters are represented by different bit patterns than their uppercase counterparts.

For example, a is represented by 1100001, and b is 1100010.  Okay, what do you think c and d are?

Later, we’ll see a table showing how every symbol, whether it be a letter, a digit, or something like punctuation, is represented.

 

For now, though, have some fun with the text conversion website I made for you. 

See how certain messages are encoded using the ASCII code.

If that gets boring, try to guess what all that hex business is at the bottom of the page.

That’ll keep you busy!


 

Hexadecimal notation

 

The word “computer” is 1100011 1101111 1101101 1110000 1110101 1110100 1100101 1110010.

You can see already that any amount of text (or anything else) is going to expand into a long binary pattern. 

It’s no wonder that hexadecimal notation is used as a shorthand for binary.

Perhaps you were able to figure out exactly how in the previous section. 

If not, stay with me.

 

Recall that for any positive integer n, there are 2n n-bit-patterns.

In particular, there are sixteen 4-bit patterns.  You’ll see them in Table 2.2 on page 25.

(If a pattern has fewer than 4 bits, fill in extra bits on the right.  For example, 0001 for 1).

 

Alongside each binary pattern in Table 2.2 is a symbol known as a hexadecimal digit. 

They are like the digits 0 thru 9 that you and I use to denote numbers in decimal (base ten).

But hexadecimal is base sixteen so it uses sixteen symbols, not ten.

Hex uses the ten symbols 0 thru 9, plus the additional six symbols A thru F.

 

Let’s remember that the c in “computer” is 1100011 in ASCII and that it appeared as 63 in hex.

Why was that?

It is because we take the rightmost 4 bits 0011 and use Table 2.2 to replace them with hex digit 3.

 

That leaves the 3 bits 110 at the front, right? 

Let’s pad that with a 0 on the left to make it 4 bits:  0110 and use Table 2.2 again to find hex digit 6.

Thus, 01100011 is

             6     3     in hex (which we usually write as x63).

 

Need practice?  Say “yes” (or say “79 65 73”) and play with this site I made for you.

 

Now, what if I told you to memorize Table 2.2?  You’d be unhappy (to put it politely).

So you don’t have to – because you’re about to learn how to generate it yourself.

 

Imagine that I had four coins I could give you: an 8-cent piece, a 4-cent, a 2-cent, and a 1-cent.

I might give you all four, in which you would earn a hefty 15 cents, or I might give you none and you’re unhappy again.

We could record each of the 16 possibilities with 4 bits (or with the 4 light switches in our classroom).

If we record 1111, that means I gave you an 8, a 4, a 2, and a 1, so 15.  The other end of the spectrum is 0000, unhappy.

 

So what is 1001, for example?  You got an 8 and a 1, so 9 cents earned.  Is this making cents (uh… sense?)

Just think of each bit slot as representing yes/no to whether I gave you that particular coin! 

 

Of course, sometimes you’ll come up with something like 1101, which is thirteen cents, right?

Just remember that the hex digit for thirteen is D (A for ten, B for eleven, and so on…).

 

This works in reverse, of course.

Suppose you want to convert x3B to binary?

I can give you 3 cents by saying no to 8, no to 4, yes to 2, yes to 1.

So 3 is 0011.

What about B?  B represents eleven, which is 8+2+1 (no 4), so that’s 1011 for yes-no-yes-yes.

 

So x3B is 00111011 in binary.  Hopefully our web site will agree.

Practice again with the web site and your new found knowledge of this 8-4-2-1 system.

And as for Table 2.2 – you can forget about it!

 

Very briefly, I’m willing to bet you can learn all of octal on your own. 

Octal is a base 8 system.

There, we use 3 bits, not 4.  I give you a 4, a 2, or a 1 (let’s call it a 4-2-1 system).

(I don’t have any 8-cent pieces now, ok?)

So you can get as much as 111, which is 7, or as little as 000, which is an unhappy 0.

 

What, then is x3B in octal? 

Well, we just saw x3B expands to 00111011 in binary.

Now consider 3-bit segments from right to left, beginning with the ending 011.

011 is 3 because it signifies no to 4, yes to 2, yes to 1.

Similarly, 111 is 7 and 000 is 0. 

 

So x3B is octal 073, or as it’s often written, o073 (o for ‘octal’).

Hopefully this web site I developed will agree.

Practice more examples yourself!

 

More about the ASCII table…

 

Recall that every keyboard symbol has a 7-bit ASCII code assigned to it.

And we now know that each 7-bit pattern can be written with 2 hex digits.

 

The ASCII table shows these hex patterns. 

It also groups the symbols very conveniently.

For example,

1. Notice that the digits appear together as a group (they are ‘contiguous’).

2. Notice that the upper-case letters are contiguous as well, beginning about halfway down the page.

Where are the lower-case letters?  Are they contiguous?   Is it good that the letters appear in the order they do?

 

This should add some insight to the string comparisons you did in BlackBox.htm.

See what happens there when you enter bob Ziggy Tom ted and push the Alphabetize button.

There is a method to this madness!

 

 

 

Representing Color in Binary

 

First, let’s think about dark versus bright using the 4 light switches in our classroom.

If all switches are off, that’s 0000 and it’s really dark. 

What color should that be?

If all switches are on, that’s 1111 and it’s really bright. 

What color is that? (And, no, you’re not getting 15 cents from me).

 

So you start to see how we might think of bits giving us 16 shades of grey ranging from all darkness (black) to full brightness (white).

But we want color TV, not black-and-white.

What about color?

 

Every color is composed of some amount of each of the three primary colors Red, Green, and Blue.

First, I’ll tell you that the amount of each primary color is represented by two hex digits.

So the amount of Red is anywhere from 00 (no red) to FF (the brightest, most intense, Full Force Red).

How many bits are used to represent the amount of red?  How many shades of red is that?

 

If your answers were 8 bits and thus 256 shades, you are golden! (which is some combination of green and red, I think).

Similarly, there are 256 shades of Green, and 256 of Blue.

How many colors can be represented all told?   Please think about this and ask me if you can’t figure it out!

It’s more than you’ll have names for, that’s for sure!

 

Have fun playing with this site I developed for you that allows you to mess with color and its representations.

Start by using buttons at the top.  Random colors are especially cool, though sometimes sickly and often putrid.

Then note that you can enter some quantity 0 to 255 for each of Red, Green, and Blue at the bottom.

If you do this, press the Requested Color button to see what you’ve created.

 

EXERCISE:  Use the Requested Color button and the Table 3.4 on page 67 to change the background color to

Yellow, then Cyan, and then Magenta.  Try also to change the background color to what the textbook calls Green.

What do you think of Green?