Don't like this style? Click here to change it! blue.css

Class 2: Getting bit-ty with it.

So I want to jump right in and explore the various types of integers in C++. It is a strongly typed language so every variable needs to be declared as some type. There is an auto type which can let you break this a little bit. But for now let's look at the various ways of storing integer values:

A warm-up

Enjoy seeing the results of un-initialized integers. If you run multiple times you'll get different values too:

By the way, today, we'll take a break whenever the Pomodoro tells us too.

C++ integers and bit lengths

Mini-Task 1: use the pattern above to look at the binary representation of 1025. Try it first saved as an int and then as a char.

Group Thought-Experiment if you were to make your own C++ and wanted to have 8-bit numbers. How would you use the bits to allow for negative numbers?

Mini-Task 2: use the pattern above to look at the binary representation of -1025. Try it first saved as an int and then as a char.

Here is a snippet showing the bit choices that are made by C++ for "signed" integers.

Any of the integer types can be signed or unsigned. Signed allows negatives and it stores basically as 1111111 - theint + 1. More precisely they use the "bit-flipped" value and add 1.

To clarify the bit-flipped version of a number is where you swap 1s and 0s. You can do this in C++ with the ~ command.

Mini-Task 3: What do you expect the bit-flipped version of 11001100 to be? When you think about this, what do you have to know to get this right? Can you use our tricks (and ~X) to confirm your suspicions?

Declaring something "unsigned"

Mini-Task 4: In this code snippet what happened? Can you alter it to create a similar issue for a signed int?

Notice how I slipped an if(){} statement in there. I bet you're cool with it.

An ASCII aside:

The reason we use char as a small integer type is because single "characters" are saved as 8-bit integers. All of the values from 00000000 to 01111111 (0 to 127) have a universal translation from integer to "ASCII character". Here is a conversion chart:

Stolen ASCII chart

DIG THIS: it isn't the most effective thing in the world but you can store an integer into a char then print the character rather than the integer.

Mini-Task 5: The name "Andy" in ASCII is 65, 110, 100, 121. Save those numbers into four variables and print "Andy" to the screen in that crazy way.

climits functions

You might have noticed that I included the climits header in our first snippet. That's a nice module for getting specific bounds on the size of your integers. Here is a list of what it provides, and some default values.

Mini-Task 6: Using climits, test to see what the largest unsigned long is in your cloud9 workspace.

A string contains a contiguous sequence of characters in memory with an ascii \0 (that is NULL) as the final character. So does an "array" of chars. But a string does more, it has methods, is managed, etc.

A Pseudo-Goal

So I hesitate to have lecture 2 involve real 0s and 1s. That might rub some folks as too far from being interesting. Here is a (contrived) way for you to kind of care.

You can store almost anything as a series of 0s and 1s, particularly if you make a system. Here is a secret bit-encoding that we can share to make a party trick out of. (Dorky parties only...)

Consider the first 5 primes: 2, 3, 5, 7, 11. I'll make a 5-bit number which encodes an integer I want to store and each digit is the remainder mod that prime. For instance 01100 is divisible by 2, 7, and 11, and has remainder 1 when divided by 3 and remainder 1 when divided by 5. (This number is 616.)

Kinda Silly Mini-Task: what is 10000 in my crazy system?

What I wanted to get across is that you can use individual bits to carry information in a very compact way. (If you are feeling motivated google information theory one day.)

GROUP TASK: design an 8-bit RPG character. I don't mean a sprite (although you are welcome to make one). I mean: come up with a set of characteristics that could be stored with only 8 0s and 1s.

Adventure Time

Working with Bits

So now your goal is to build some code to make your adventurer come to life. (We've got some NES stuff going on here...)

Let's say that the top bit was the gender of your adventurer (1 for female and 0 for male). Then this is a naive way to detect gender:

This method would not extend to checking lower bits very well either.

Bit Tricks (IMPORTANT!)

  1. ~value will flip every bit
  2. value << integer "Left-Shifts" the bits by integer "spaces" this creates 0s
  3. value >> integer "Right-Shifts" the bits by integer "spaces" this creates 0s
  4. value1 & value2 does a bit-wise AND between the two values.
  5. value1 | value2 does a bit-wise OR between the two values.
  6. value1 ^ value2 does a bit-wise XOR (exclusive or) between the two values.

Observe:

Mini-Task 7: What 8-bit value can AND with any value and return that value? Answer the same question but for OR.

If I consider my top bit as gender, and the next two bits as weapon (Axe, Sword, Wand, Bow), then the following will tell me gender and weapon:

WARNING: if we were really building a hero-based game we would use classes. However, the ability to get and change which ever BIT you want is very powerful. Also, when it comes to sending data over networks every bit counts. For evidence go to google.com and inspect their javascript (it is packed very tight, no wasted characters).

INVITATION: Ask me about fast arithmetic mod 2!

Third-Party Exploration: Read this Stack Overflow Q/A and try to really understand it.

Weekend-Challenge!

Build some more code to display the stats of your 8-bit hero, it is the first problem of your first homework.

LIFEHACK

Self-Teaching Mini-Task Use google to find out how to extract a substring from a string. Now use it to figure out how to make a string uppercase.

Take-Away Lessons

So we learned all about bit-flipping. We learned about the various integer types in C++. We quietly learned how a C++ if statement works. We also learned how to use the internet to solve a problem we didn't know the answer to. In our subconscious we saw that operators can have different meanings in different contexts (this is called overloading an operator) (think >> for strings or integers).

If Poorly Timed:

Talk about floats, doubles, ++X, X++, for