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 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 (use filename lab02p1.cpp
)
that converts a number from binary to decimal format and prints out the
decimal value of that number. In particular your program will:
$ ./lab02p1 Enter a 4-bit binary number: 1101 In decimal 1101 = 13
submit as: ~/bin/submit -c=IC210 -p=lab02 lab02p1.cpp
You will write a program (use
filename lab02p2.cpp
) 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 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 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 lab02p3.cpp
) that reads in a chmod triple
(e.g. 754) and print a summary of the permissions as shown in the
example execution below.
$ ./lab02p3 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