## 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 is 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 the contribution of that digit to the value of the number is d*bc. For example the number 123 base 10 (written 12310) is 1*102 + 2*101 + 3*100, which totals 123. For a less obvious example, 1238 is 1*82 + 2*81 + 3*80, which 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). 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 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. Read the number in as four separate characters.
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):

$./lab1 Enter a 4-bit binary number: 1101 In decimal 1101 = 13 ## Part 2: Converting Back to Binary A few questions to ask yourself: If x = b3b2b1b0 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/(23), or 1. You will write a program 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. 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. $ ./lab2
Enter a number between 0 and 15: 13
13 in binary is 1101

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.

## Going Further - Part 3: Unix Permissions

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 the access permissions for "Other", meaning users other than the owner and members of the same group.

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

\$ ./lab3
Permissions: 754

User: