This homework is meant to solidify your understanding of the binary representation of numbers. Additionally, you will get a bit of experience manipulating numbers using C’s binary operations.
The task is to write a program that accepts a user-provided value and then “rotates” the bits of that number a by a specified amount. A rotation is a shifting of the bits in a number, with any bits shifted beyond the number’s range placed on the other side. Here is an example of a right rotation on an 8 bit number.
A left rotation goes in the opposite direction:
To add bits into an existing number, use the binary or operator “|”.
For this homework, you will be rotating ints, which are 32 bits. You will read the int using the atoi function, as we’ve done in class. Note: We will not be rotating negative numbers, as a right shift will left pad with 1s to keep the value negative. That would require us to manually set those bits to 0. If that doesn’t make any sense, just ignore it; none of the test inputs will attempt to rotate the bits of a negative number.
The program will take two arguments. The first is the number to rotate, and the second is the amount of the rotation, with negative values meaning a left rotation and positive values meaning a right rotation. Notice that rotations greater than the size of an int will wrap all the way around, so you can simplify your code by using the modulo operator on the rotation amount.
Your program will print out the bits of the rotated number, in groups of 8 bits for readability.
You program must be named “hw2.c”, and it must have a comment block at the top, that looks something like this:
/*
* Author: your name
* Netid: your netid
* Aid: If in doubt, list anything you referenced. A stack overflow post, chatgpt, gemini, your friend Bob, etc. If referring to an LLM, you must included a link to a particular session. Just saying "I asked chatgpt" isn't a get out of trouble free statement. If two submissions are nearly identical and nothing is listed, I will have to assume that there was copying with an attempt at obfuscation.
*/Return values:
Upon success, print out the binary integer in 8 bit groups and then return 0. If the user does not provide enough inputs, do not print anything and return 1.
Some examples:
Compile your code with: gcc hw2.c -o hw2
$ gcc hw2.c -o hw2 $ ./hw2 1 -1 00000000 00000000 00000000 00000010 $ ./hw2 1 1 10000000 00000000 00000000 00000000 $ ./hw2 255 -8 00000000 00000000 11111111 00000000 $ ./hw2 255 32 00000000 00000000 00000000 11111111 $ ./hw2 256 0 00000000 00000000 00000001 00000000 $ echo $? 0 $ ./hw2 blub $ echo $? 1
Testing
You are provided with a testing script named “test_hw2.sh”. Put this
script into the same directory as your hw2.c file and run it with the
command: bash test_hw2.sh. It will run, test for errors,
and print out your score. This will work on the ilab machines, but I
cannot guarantee that it will work on your personal computer.
Submission
You should submit your hw2.c file through Canvas.