C, Binary, and Hex

This assignment consists of two parts. The first part involves manually converting among binary, hexadecimal, and decimal, and second part is a program to be modified.

You may want to consult the Power Point presentation in the Teams Files tab.

Part 1 - Decimal, Binary, and Hexadecimal

Work out these conversions manually. You could do it on paper and submit a picture of your work, or do it on a computer and put your work in a plain text file or exported PDF. You may check your results using one of the many converters available online, but for credit you must show your work.

Converting from Binary

Problem 1: Convert the following from binary to both decimal and hexadecimal:

Converting from Hexadecimal

Problem 2: Convert the following from hexadecimal to both decimal and binary:

Part 2 - Bit Shifting and Masking in C

For this part you will write some C code to use bit shift operations and the bitwise “and” operation on an integer. For this program, it does not matter if the program is written within your Linux VM or not.

If your VM is Ubuntu, you can install the development command line tools like so:

sudo apt install build-essential git

See the “Class Code” section of the site for the example code from class which illustrates the use of bit shifting and bitwise and.

Bit Masks

Bit masking is the technique of using bitwise operations to retrieve or manipulate the bits of a stored value.

For example, to get the 4 least significant bits of a number, you can perform a bitwise “and” (the & operator) with 0xF, which is 1111. Assuming the original number is an 8-bit number, that can be visualized like this:

01101110 &
00001111
--------
00001110

In this case, 0xF is called the mask.

Provided Code

You are provided with a file named bit_masking.c. Recall that you compile a C program on the command line like so:

$ gcc bit_masking.c -o bit_masking

Then you can run it like this:

$ ./bit_masking

Compile the provided file and run it. You will be asked to enter a non-negative integer, and it will print the decimal and hex representations:

$ ./bit_masking
Enter a non-negative integer: 1234
decimal: 1234
hex:     0x4D2

Your Changes

You are to add code to this file so that it prints out the hex representations of each of the 4 bytes in the stored representation, from most to least significant. So after your changes, the output should look like this:

$ ./bit_masking
Enter a non-negative integer: 1234
decimal: 1234
hex:     0x4D2
individual bytes, from most to least significant:
0x00
0x00
0x04
0xD2

To get one of the bytes from the number you need a mask that is 8 1’s (0xFF). You can then shift the number so that the byte you want is in the least significant 8 bits of the number, and then apply the mask.

In the example in class, separate code was written to print out each byte. For full credit on this assignment, your code must instead use a for loop and print out one byte a time in the body of the loop. This means that the number of bytes to shift needs to be calculated for each iteration of the loop.

git-keeper will be picky about the output being 2 hex digits for each byte. The printf() conversion specification for 2-digit hex is %02X.

Submission

Place the file containing your solutions for part 1 in the git-keeper repository. Add your changes for this file and for bit_masking.c and commit and push back to git-keeper.

Your code for part 2 will be based on the following: