Edit, compile, and run the following programs on the UNIX shell:
Write a program that takes in six commandline arguments and has four functions (described below) that use bitwise operators.
Here is an outline of the program with the four function prototypes described in the instructions:
#include <stdio.h>
#include <string.h>
#include "printbits.h"
//function prototypes
int twosComplement(unsigned int);
unsigned int charPacker(unsigned char, unsigned char, unsigned char, unsigned char);
void intChopper(unsigned int, unsigned char*, unsigned char*, unsigned char*, unsigned char*);
unsigned int circleLeft(unsigned int, unsigned int);
int main(int argc, char *argv[]){
//error checking
//function calls
return 0;
}
//function definitions
HERE IS THE PRINTBITS.C
#include <stdio.h>
#include "printbits.h"
/*
Displays the bit pattern for a 32-bit int
number: is the 32-bit integer to be displayed
*/
void printbits(unsigned int number){
//loop counter
int i = 0;
/*
A mask is used by bitwise AND to determine
if a specific 1 or a 0 is present.
Masks are used to select some bits
(using 1s - which returns 1, if 1, and returns 0, if 0)
or hide other bits
(using 0s - which make everything zero).
This starts with a 1 at the 31st (leftmost) position,
followed by 31 zeros.
*/
unsigned int mask = 0x80000000;
//loop through the 32 bits
for(i=31;i>=0;i--) {
//printf("i=%i, mask=0x%x",i,mask);
//determine if a 1 or 0 exists at each bit location
if(0 != (number & mask)){
printf("1");
}
else{
printf("0");
}
//add a space for easy reading
if((24==i) || (16==i) || (8==i)){
printf(" ");
}
//shift bits in mask to the right by one bit
mask = mask >> 1;
}//end of for loop
/*
Display the unsigned integer.
%u is the format character for unsigned integers.
%d is for signed integers - use if you want to display negative numbers.
Leave room for 8 extra spaces for hexadecimal numbers.
*/
printf(" = 0x%.8X = %d \n", number, number);
}//end of function
HERE IS THE PRINTBITS.H
void printbits(unsigned int);
HERE IS THE MAKEFILE I MADE
CXX=gcc
CXXFLAGS= -std=c99 -g -Wall -Wshadow -Wpedantic -Wvla -Werror
SOURCES=program.c printbits.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=./a.out
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(OBJECTS) -o $@
#add -lm after @ to include c math library
clean:
rm -f $(OBJECTS) $(EXECUTABLE)
.c.o:
$(CXX) $(CXXFLAGS) -c -o $@ $
OUTPUT:
./program ERROR: Please enter the executable name, followed by FOUR characters and then TWO integers on the commandline. You entered 1 argument(s). ./program A B C D 1 2 3 ERROR: Please enter the executable name, followed by FOUR characters and then TWO integers on the commandline. You entered 8 argument(s). ./program AA B C D 1 2 ERROR: Please enter a character for character #1 on the commandline. You entered 2 characters: AA ./program A B C DDDDD 1 2 ERROR: Please enter a character for character #4 on the commandline. You entered 5 characters: DDDDD ./program A B C D E 2 ERROR: You are missing an integer argument. You entered: E ./program A B C D 1 F ERROR: You are missing an integer argument. You entered: F ./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 unsigned integer variable in bit format: 01000001 01000010 01000011 01000100 = 0x41424344 = 1094861636 function #3: Input is unsigned integer: 00000000 00000000 00000000 00000010 = 0x00000002 = 2 Output is four characters: 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000010 = 0x02 = '' function #4: Input is unsigned integer: 00000000 00000000 00000000 00000001 = 0x00000001 = 1 Rotated 2 bit(s) to the left. Output is unsigned integer: 00000000 00000000 00000000 00000100 = 0x00000004 = 4 ./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 unsigned integer variable in bit format: 01110111 01111000 01111001 01111010 = 0x7778797A = 2004384122 function #3: Input is an unsigned integer: 00000000 00000000 00000000 00001000 = 0x00000008 = 8 Output is four unpacked characters: 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00001000 = 0x08 = ' (note: 8 is backspace) function #4: Input is unsigned integer: 00000111 01011011 11001101 00010101 = 0x075BCD15 = 123456789 Rotated 8 bit(s) to the left. Output is unsigned integer: 01011011 11001101 00010101 00000111 = 0x5BCD1507 = 1540166919 ./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 unsigned integer variable in bit format: 01000001 01000010 01000011 01000100 = 0x41424344 = 1094861636 function #3: Input is an unsigned integer: 00000000 00000000 00000000 00000000 = 0x00000000 = 0 Output is four unpacked characters: 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000000 = 0x00 = '' function #4: Input is unsigned integer: 00000000 00000000 00000000 00000000 = 0x00000000 = 0 Rotated 0 bit(s) to the left. Output is unsigned integer: 00000000 00000000 00000000 00000000 = 0x00000000 = 0 ./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 unsigned integer variable in bit format: 00110001 00110010 00110011 00110100 = 0x31323334 = 825373492 function #3: Input is an unsigned integer: 00000000 00000000 00000000 00100000 = 0x00000020 = 32 Output is four unpacked characters: 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00000000 = 0x00 = '' 00100000 = 0x20 = ' ' function #4: Input is unsigned integer: 00000000 00000000 00000000 00000110 = 0x00000006 = 6 Rotated 32 bit(s) to the left. Output is unsigned integer: 00000000 00000000 00000000 00000110 = 0x00000006 = 6 ./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 unsigned integer variable in bit format: 01100001 01100010 01100011 01100100 = 0x61626364 = 1633837924 function #3: Input is an unsigned 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 unsigned integer: 00000000 00000000 00000000 00001000 = 0x00000008 = 8 Rotated 893542945 bit(s) to the left. Output is unsigned integer: 00000000 00000000 00000000 00010000 = 0x00000010 = 16
This code will work according to your assignment. please go threw it and output.
#include <stdio.h>
#include <stdlib.h>
int TowsComp(unsigned int num)
{
return (~num)+1;
}
void PrintBits(unsigned int num) // print bits
{
int i = 0 ;
int count =0;
for(i=31 ;i>= 0 ;i--)
{
if(count%8 == 0)
printf(" ");
num>>i & 1 ? printf("1"): printf("0");
count++;
}
printf(" = 0x%X = %u",num,num);
printf("\n");
}
void PrintBitsChar(unsigned int num) //print character bits
{
int i = 0 ;
int count =0;
for(i=7 ;i>= 0 ;i--)
{
if(count%8 == 0)
printf(" ");
num>>i & 1 ? printf("1"): printf("0");
count++;
}
printf(" = 0x%X = '%c'",num,num);
printf("\n");
}
unsigned int Pack4Char(unsigned char c1 ,unsigned char c2 ,unsigned
char c3 ,unsigned char c4 ) //pack characters in int
{
unsigned int num = 0;
num = num | (c4);
num = num | (c3 << 8);
num = num | (c2 << 16);
num = num | (c1 << 24);
return num;
}
void Upack4Char(unsigned int num) //unpack character from int
{
unsigned char c1 = 0;
unsigned char c2 = 0;
unsigned char c3 = 0;
unsigned char c4 = 0;
c4 = num & 0xFF;
c3 = (num >> 8) & 0xFF;
c2 = (num >> 16) & 0xFF;
c1 = (num >> 24) & 0xFF;
}
unsigned int LelfShift(unsigned int x , unsigned int n) // left
shift x by n
{
x = (x << n) | (x>> (32-n));
return x;
}
int main(int argc , char ** argv)
{
if (argc < 2 && argc >= 6) // check arguments are ok
or not
{
printf("Usage : ./a.out 4-char 2-numbers ....\n");
return 0;
}
int i =0 , j =0;
unsigned int num[2] ={0};
for(i=1 ; i < argc ;i++) // check arguments and convert in to
int
{
if(atoi(argv[i]) == 0)
{
if( i<=4 )
continue;
printf("argument should be 4 latter followed by 2
intiger\n");
return 0;
}
else
{
num[j++] = atoi(argv[i]);
}
}
printf("function #1\n");
printf("Input is unsigned integer\n");
PrintBits(num[0]);
printf("Output is 2's compliment of the integer:\n");
PrintBits(TowsComp(num[0]));
printf("\nfunction #2\n");
printf("Input is four characters\n");
PrintBitsChar(argv[1][0]);
PrintBitsChar(argv[2][0]);
PrintBitsChar(argv[3][0]);
PrintBitsChar(argv[4][0]);
printf("Output is packed unsigned integer variable in bit
formate\n");
PrintBits(Pack4Char(argv[1][0],argv[2][0],argv[3][0],argv[4][0]));
printf("\nfunction #3\n");
printf("Input is unsigned integer\n");
PrintBits(num[0]);
printf("Output is four unpacked characters\n");
Upack4Char(num[0]);
printf("\nfunction #4\n");
printf("Input is unsigned integer\n");
PrintBits(num[0]);
printf("Output is unsigned interger\n");
PrintBits(LelfShift(num[0],num[1]));
}
Output:

Edit, compile, and run the following programs on the UNIX shell: Write a program that takes...
(Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....
The left-shift operator can be used to pack two character values
into an unsigned integer variable. Write a program that inputs two
characters from the keyboard and passes them to function
packCharacters. To pack two characters into an unsigned integer
variable, assign the first character to the unsigned variable,
shift the unsigned variable left by 8- bit positions and combine
the unsigned variable with the second character using the bitwise
inclusive OR operator. The program should output the characters in...
Write a C program that uses the bitwise shift operators to shift the bits to the right >> or the left > m; /* This shifts m bits to the right, and the m least significant bits are lost.*/ The following statements are the same. num = num >> 3; num >>= 3; Show the operation in binary by calling the following function as defined in 3.1, void to_binary(unsigned int n); The function converts decimal to binary and outputs the...
Please complete the following problem in C. No source code is provided. The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned int variable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the...
write a C program!! Q2 and Q3
Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...
PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...
Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...
Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...
Application Problem: Answer the following questions at the bottom of the worksheet: You are configuring a microcontroller (uC) to sample a signal connected to an input pin. Part of the initial setup requires that you clear (turn off) bits #17 and #2 in a 32-bit register, while leaving all other bits unchanged. To work with specific bits, we typically use a second number, called a mask, which has the bit positions we need to alter set to 1, and all...
C PROGRAM
When you print out the old and new bitsets, which are of type
unsigned char, please use the %p control character in the printff
statement rather than %x or %d or %c. The compiler will complain,
but we don't always listen to them anyway. The difference is it
will print the bitset out in hex with a preceeding %0x.
unsigned char bitset = 0x14 ;
printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14,
which is what...