`lab02`

and make sure all two/three
source code (.cpp) files you create are in that directory.
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 the
contribution of that digit to the value of the number is
*d*b ^{c}*. For example the number 123 base 10
(written 123

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). 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 will write a program (use filename

) that converts a number from binary to
decimal format and prints out the decimal value of that
number. In particular your program will:
**lab02p1.cpp**

- 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.
- Read the number in as four separate characters.
- Convert the four bits to a single decimal number.
- Print out the decimal value of the number.

$ ./lab02p1 Enter a 4-bit binary number: 1101 In decimal 1101 = 13

**submit as:** `~/bin/submit -c=IC210 -p=lab02 lab02p1.cpp`

A few questions to ask yourself:
If x = b_{3}b_{2}b_{1}b_{0}
is a 4-bit binary number, what is x/2? (Remember, this is int
division!) What is x%2? What is
x*2? What is x/4? What is x%4? What is x*4? Why 2 and 4?
What's interesting about those?

Just as one might want to convert from binary to decimal, one might
want to perform the conversion the other direction (decimal to
binary). The basic principle is the same, except instead of
multiplying by powers of two, we need to divide (and mod) by the
powers of two. For example, if the decimal number is 11 and we want
to convert to a 4 bit binary number, then the leftmost bit (column 3)
is 11/(2
You will write a program (use
filename

) that converts a number from decimal to
binary format. You will then print out the binary value of that
number. You need to figure out how to perform the conversion. You
know the first step.
**lab02p2.cpp**

- 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.
- Read in the number as an integer.
- Convert the number into four binary digits.
- Print out the four digits.

$ ./lab02p2 Enter a number between 0 and 15: 13 13 in binary is 1101

**submit as:** `~/bin/submit -c=IC210 -p=lab02 lab02p1.cpp lab02p2.cpp`

**Hint:** If you're having trouble thinking about this,
try doing a base 10 version first - i.e. write a program
that reads a 4-digit decimal number into an `int`

variable and then picks apart the `int`

to figure
out what the original decimal digits were - perhaps printing
each digit out separated by a space. This should give a
good analogy to what you're trying to do with the binary
version.

chmod 754 foo.shThe 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 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

) that reads in a chmod triple
(e.g. 754) and print a summary of the permissions as shown in the
example execution below.
**lab02p3.cpp**

$ ./lab3 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 lab02p1.cpp lab02p2.cpp lab02p3.cpp`