Assignment Instructions

  1. Edit, compile, and run the following program on the UH UNIX shell:
    1. To get input from the user, enter these commandline arguments: four characters (any ASCII character) followed by two integers. Otherwise, print error message and end program.
    2. To convert a string to an integer, use the function atoi(). For example: int integer1 = atoi(argv[5]);
    3. Use data types "unsigned int" and "unsigned char". Be careful with looping, as there will be no negative integers.
    4. Function #1: This function calculates the two's complement. The function's parameter is one unsigned integer (unsigned int). The function returns a signed integer (int), which is the two's complement of the parameter. Use the one's complement operator (~) to get the one's complement. Add one (1) to the to the one's complement to get the two's complement.
    5. Function #2: This function packs four characters into one integer. The function's parameters are four unsigned characters. The function returns an unsigned integer. Use the left shift operator (<<) and the bitwise inclusive OR operator (|) to pack four 8-bit characters into a 32-bit integer.
    6. Function #3: This function unpacks an integer into four characters. The function's parameters are an unsigned integer (which is input to the function), and four character pointers (which are the outputs of the function). This function does not return anything. Use the right-shift operator (>>) and the bitwise AND operator (&) with a mask to separate the 32-bit integer into four 8-bit parts, and store these four 8-bit parts in the four 8-bit characters. In other words, take one 32-bit integer (int) and convert it into four 8-bit characters (char).
      ***** Here is an example function prototype:
      void unpackCharacters(unsigned int, unsigned char*, unsigned char*, unsigned char*, unsigned char*);
    7. Function #4: This function is a circular left shift of the bits. The function's parameter are two unsigned integers: integer1 and integer2. The function returns an unsigned integer. This function will return an unsigned integer with integer2's bits rotated to the left integer1's number of times. You may need to use a mask, left shift operator (<<), right shift operator (>>), the bitwise AND operator (&), and the bitwise inclusive OR operator (|).
      *****
      In other words, when the bits are shifted to the left by one, the bit in the leftmost position will be moved to the rightmost position. When the bits are shifted to the left by 2, the 2 bits in the leftmost position will be moved to the rightmost position. Etc.
      *****
      One possible solution is for the circular left shift operation is:
      circularLeftShift(n, x) = (x << n) OR (x >> 32-n)
      Replace the "OR" with the inclusive or operator (|)
    8. Here is an outline of the program with the four function prototypes described in the instructions: LastnameFirstname16.c
    9. Your outfit should match the output below. You can use the printbits() function in your code; however, you also need to create your own function that displays the eight bits in a character.
    10. Email your makefile and C program to the instructor, or show it to the instructor in class.
  2. For the submission guidelines and the grading guidelines, see the syllabus.

Example I/O

walbritt@uhx01:/home/27/walbritt> ./program                                         
ERROR: Please enter the executable name, followed by FOUR characters, followed by TWO integers on the commandline.
You entered 1 argument(s).


walbritt@uhx01:/home/27/walbritt> ./program A B C D 1 2 3                           
ERROR: Please enter the executable name, followed by FOUR characters, followed by TWO integers on the commandline.
You entered 8 argument(s).


walbritt@uhx01:/home/27/walbritt> ./program AA B C D 1 2                            
ERROR: Please enter a character for character #1 on the commandline.
You entered 2 characters: AA


walbritt@uhx01:/home/27/walbritt> ./program A B C DDDDD 1 2                         
ERROR: Please enter a character for character #4 on the commandline.
You entered 5 characters: DDDDD


walbritt@uhx01:/home/27/walbritt> ./program A B C D E 2                             
ERROR: You are missing an integer argument.
You entered: E 


walbritt@uhx01:/home/27/walbritt> ./program A B C D 1 F                             
ERROR: You are missing an integer argument.
You entered: F 


walbritt@uhx01:/home/27/walbritt>./program A B C D 2 1                   

function #1:
Input is unsigned integer:
00000000 00000000 00000000 00000010 = 0x00000002 = 2 
Output is the two's complement of the integer: 
11111111 11111111 11111111 11111110 = 0xFFFFFFFE = -2 

function #2:
Input is four characters:
01000001 = 0x41 = 'A' 
01000010 = 0x42 = 'B' 
01000011 = 0x43 = 'C' 
01000100 = 0x44 = 'D' 
Output is packed unsiged integer variable in bit format: 
01000001 01000010 01000011 01000100 = 0x41424344 = 1094861636 

function #3:
Input is an unsiged integer: 
00000000 00000000 00000000 00000010 = 0x00000002 = 2 
Output is four unpacked characters: 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000010 = 0x02 = '' 

function #4:
Input is unsiged integer: 
00000000 00000000 00000000 00000001 = 0x00000001 = 1 
Rotated 2 bit(s) to the left. 
Output is unsiged integer: 
00000000 00000000 00000000 00000100 = 0x00000004 = 4 


walbritt@uhx01:/home/27/walbritt>./program w x y z 8 123456789                                 

function #1:
Input is unsigned integer:
00000000 00000000 00000000 00001000 = 0x00000008 = 8 
Output is the two's complement of the integer: 
11111111 11111111 11111111 11111000 = 0xFFFFFFF8 = -8 

function #2:
Input is four characters:
01110111 = 0x77 = 'w' 
01111000 = 0x78 = 'x' 
01111001 = 0x79 = 'y' 
01111010 = 0x7A = 'z' 
Output is packed unsiged integer variable in bit format: 
01110111 01111000 01111001 01111010 = 0x7778797A = 2004384122 

function #3:
Input is an unsiged integer: 
00000000 00000000 00000000 00001000 = 0x00000008 = 8 
Output is four unpacked characters: 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00001000 = 0x08 = ' 

function #4:
Input is unsiged integer: 
00000111 01011011 11001101 00010101 = 0x075BCD15 = 123456789 
Rotated 8 bit(s) to the left. 
Output is unsiged integer: 
01011011 11001101 00010101 00000111 = 0x5BCD1507 = 1540166919 



walbritt@uhx01:/home/27/walbritt> ./program A B C D 0 0                             

function #1:
Input is unsigned integer:
00000000 00000000 00000000 00000000 = 0x00000000 = 0 
Output is the two's complement of the integer: 
00000000 00000000 00000000 00000000 = 0x00000000 = 0 

function #2:
Input is four characters:
01000001 = 0x41 = 'A' 
01000010 = 0x42 = 'B' 
01000011 = 0x43 = 'C' 
01000100 = 0x44 = 'D' 
Output is packed unsiged integer variable in bit format: 
01000001 01000010 01000011 01000100 = 0x41424344 = 1094861636 

function #3:
Input is an unsiged integer: 
00000000 00000000 00000000 00000000 = 0x00000000 = 0 
Output is four unpacked characters: 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 

function #4:
Input is unsiged integer: 
00000000 00000000 00000000 00000000 = 0x00000000 = 0 
Rotated 0 bit(s) to the left. 
Output is unsiged integer: 
00000000 00000000 00000000 00000000 = 0x00000000 = 0 


walbritt@uhx01:/home/27/walbritt> ./program 1 2 3 4 32 6     

function #1:
Input is unsigned integer:
00000000 00000000 00000000 00100000 = 0x00000020 = 32 
Output is the two's complement of the integer: 
11111111 11111111 11111111 11100000 = 0xFFFFFFE0 = -32 

function #2:
Input is four characters:
00110001 = 0x31 = '1' 
00110010 = 0x32 = '2' 
00110011 = 0x33 = '3' 
00110100 = 0x34 = '4' 
Output is packed unsiged integer variable in bit format: 
00110001 00110010 00110011 00110100 = 0x31323334 = 825373492 

function #3:
Input is an unsiged integer: 
00000000 00000000 00000000 00100000 = 0x00000020 = 32 
Output is four unpacked characters: 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00000000 = 0x00 = '' 
00100000 = 0x20 = ' ' 

function #4:
Input is unsiged integer: 
00000000 00000000 00000000 00000110 = 0x00000006 = 6 
Rotated 32 bit(s) to the left. 
Output is unsiged integer: 
00000000 00000000 00000000 00000110 = 0x00000006 = 6 


walbritt@uhx01:/home/27/walbritt> ./program a b c d 893542945 8      

function #1:
Input is unsigned integer:
00110101 01000010 01100010 00100001 = 0x35426221 = 893542945 
Output is the two's complement of the integer: 
11001010 10111101 10011101 11011111 = 0xCABD9DDF = -893542945 

function #2:
Input is four characters:
01100001 = 0x61 = 'a' 
01100010 = 0x62 = 'b' 
01100011 = 0x63 = 'c' 
01100100 = 0x64 = 'd' 
Output is packed unsiged integer variable in bit format: 
01100001 01100010 01100011 01100100 = 0x61626364 = 1633837924 

function #3:
Input is an unsiged integer: 
00110101 01000010 01100010 00100001 = 0x35426221 = 893542945 
Output is four unpacked characters: 
00110101 = 0x35 = '5' 
01000010 = 0x42 = 'B' 
01100010 = 0x62 = 'b' 
00100001 = 0x21 = '!' 

function #4:
Input is unsiged integer: 
00000000 00000000 00000000 00001000 = 0x00000008 = 8 
Rotated 893542945 bit(s) to the left. 
Output is unsiged integer: 
00000000 00000000 00000000 00010000 = 0x00000010 = 16