Due: 0800, next Wednesday (because Tuesday is a Monday)

Part 0: In your ic210 folder, create directory lab02

Create a directory lab02 and make sure all two/three source code (.cpp) files you create for this lab are in that directory.

Part 1: Reading Binary Numbers

As you may recall, numbers can be represented in various bases. A base describes how many unique digits are used in representing the number. For example, we mostly represent numbers in base 10; there are 10 different digits used to represent the numbers: 0,1,2,3,4,5,6,7,8,9. Numbers can be represented in many (infinite!) other bases, such as base 8. In base 8, the numbers are represented with only 8 digits: 0,1,2,3,4,5,6,7.

The actual value of a number depends on both what digits are used, and the location of the digits. Obviously, the number 123 is smaller than the number 321. In particular the farther left a digit is, the more contribution it makes to the size of the number. If we number the columns from right to left, starting with 0, then when the base is b and the digit d is in column c, the contribution of that digit to the value of the number is d*bc. For example the number 123 base 10 (written 12310) can be written as follows:

12310 = 1*102 + 2*101 + 3*100
For a less obvious example:
1238 = 1*82 + 2*81 + 3*80
So, 1238 totals 83.

The reason you're reading all this is because computers don't store their numbers in base 10, but rather in base 2 (the only digits are 0 and 1).

11012 = 1*23 + 1*22 + 0*21 +1*20 = 1*8 + 1*4 + 0*2 + 1*1 = 13
So, 11012 totals 13.

As computer programmers, we need to be able to convert numbers from the binary (base 2) format to the decimal (base 10) format. That is what you will do for the lab.

You should have a good understanding of operators / and % for integers.

You will write a program (use filename p1.cpp) that converts a number from binary to decimal format and prints out the decimal value of that number. In particular your program will:

  1. Prompt the user to enter a 4-bit binary number (Recall that binary digits are also called bits). You may assume that the number the user enters really is a binary number.
  2. Extract each bit from the 4-bit number.
  3. Convert the four bits to a single decimal number.
  4. Print out the decimal value of the number.
Here is a sample run of the program (user input in red):
$ ./p1
Enter a 4-bit binary number: 1101
In decimal 1101 = 13

submit as: ~/bin/submit -c=IC210 -p=lab02 p1.cpp

Part 2: Converting Back to Binary

You will write a program (use filename p2.cpp) that converts a number from decimal to binary format.

Background

Given an integer x, you need to find four binary digits b3, b2, b1, and b0 such that
x   =   b3*23 + b2*22 + b1*21 + b0*20   =   b3*8 + b2*4 + b1*2 + b0*1
For example, 13 = 11012 since we have
13 = 1*8 + 1*4 + 0*2 + 1*1

Answers to the following questions will help you figure out your solution to this part:

  1. What is x/4? In other words, what is the following?
    (b3*8 + b2*4 + b1*2 + b0*1)/4
  2. What is x%2? In other words, what is the following?
    (b3*8 + b2*4 + b1*2 + b0*1)%2
  3. What is (x/4)%2?
  4. What's interesting about those? Why 4 and 2?

Your program

  1. Prompt the user to enter a decimal number between 0 and 15, inclusive. You may assume that the user really does enter a number in that range.
  2. Read in the number as an integer.
  3. Convert the number into four binary digits.
  4. Print out the four digits.
$ ./p2
Enter a number between 0 and 15: 13
13 in binary is 1101

submit as: ~/bin/submit -c=IC210 -p=lab02 p1.cpp p2.cpp

Going Further - Part 3: Unix Permissions

Going Further?

In each lab, there will be parts labeled with "going further". In general, these parts will be a bit more challenging.

In Unix, file permissions are normally set with the command chmod with a three digit number as an argument. For example, if you have a file foo.sh, you might set its permissions like this:

chmod 754 foo.sh
The way this is interpreted is that each digit is a "bit mask", meaning that its numeric value is not important, rather its bit-representation is what really matters. In fact, each digit must be in the range 0-7, which means they are three-bit numbers. The first (leftmost) bit tells you whether or not read access is granted (1 means yes, 0 means no). The second gives the write access permissions. The third gives the execute (i.e. as a program) permissions. So, for example, 6 as a 3-bit number is 110 which means read is on, write is on, execute is off.

So why are there three digits? The first (leftmost) digit is for "User" permissions, which means the user who owns the file. The second digit is for "Group" permissions. Users can be placed into groups, and this specifies the access permissions for other users who are in the same group. Finally, the third digit specifies the access permissions for "Other", meaning users other than the owner and members of the same group.

Your job is to write a program (use filename p3.cpp) that reads in a chmod triple (e.g. 754) and print a summary of the permissions as shown in the example execution below.

$ ./p3
Permissions: 754

User:
read    1
write   1
execute 1

Group:
read    1
write   0
execute 1

Other:
read    1
write   0
execute 0

submit as: ~/bin/submit -c=IC210 -p=lab02 p1.cpp p2.cpp p3.cpp