Using a command line arguments (main(int argc, char * argv[])), the C program yo
ID: 3668655 • Letter: U
Question
Using a command line arguments (main(int argc, char * argv[])), the C program you write will process the appropriate command to accomplish the required set task as follows:
a. $lab3 -p 0x5
Include a procedure, void printBits(unsigned long i), that prints the bits in i. It can be invoked from the command line using the -p switch followed by a 32-bit unsigned long integer in hexadecimal form. The minimum output should appear as follows:
0000 0000 0000 0000 0000 0000 0000 0101
b. $lab3 -u 0x7 0x55
A useful implemenetation of the mathemtical concept of set is an unsigned long treated as a set of up to 32 elements.
typedef unsigned long set;
const set empty = 0x0; /* Use hexadecimal constants */
Include a procedure that does set union using bit pattersn. Caution: Because union is a C keyword, us another name.
set Union(set a, set b);
0000 0000 0000 0000 0000 0000 0101 0111
c. $lab3 -i 0x7 0x55
While we are at it, create an intersection function:
set intersection(set a, set b);
0000 0000 0000 0000 0000 0000 0000 0101
d. $LAB3 -C 0X7
We can't forget the complement function:
set complement (set a);
1111 1111 1111 1111 1111 1111 1111 1000
e. $lab3 -r 0x7 3
C does not have a rotate operator so we will implement a right rotate function that returns the value of the unsigned long x rotated to the right by n bit positions:
unisigned long rrotate (unsigned long x, int n);
f. $lab3 -s 0x5
Include a procedure that counts the number of bits set in an unsigned long. For example, the number 5 (hexadecimal), which is 0000000000000101 (binary), has two bits set. Your output should include all three pieces of information.
g. $lab3 -m 0x55
Include a procedure that will take the bits in a number and shift them to the left end. For example, 0000 0000 0000 0000 0000 0000 0101 0101 (binary) would become 1111 0000 00000 00000 00000 0000 0000 0000 (binary).
You will need to use your printBits() function to display results of all of these functions. Make the output attractive and informative.
Hints: Build the program incrementally starting with the printBits() precedure. Implement each new function and test it completely. Make sure you use the bit operations and not string modifications, etc. to complete the task.
Explanation / Answer
#include <stdio.h>
#include <string.h>
typedef unsigned long set;
const set empty = 0x0;
//Prototypes
set complement(set);
set intersection(set,set);
set bitUnion(set,set);
int countBits(set);
void printBits(unsigned long);
set rrotate(set,int);
void leftShift(set);
//Global variables
int c,i,j,k; //Loop variables
set firstdec; //stores the first hexadecimal argument from the command line
set seconddec; //stores the second hexadecimal argument from the command line
set result; //Result from performed operations
int main(int argc, char *argv[]){
if(argc < 2 ||argc > 4){ //If command line has less than 2 or more than 3 arguments
perror("Not enough or too many args provided. "); //Print an error
return(-1); //Return -1 instead of a positive number
}
else if(argv[1][1] != 'p' && argv[1][1] != 'c' && argv[1][1] != 's' && argv[1][1] != 'u' && argv[1][1] != 'i' && argv[1][1] != 'r' && argv[1][1] != 'm'){ //If the first argument isnt a valid operation
perror("Invalid procedure from command line argument! "); //Print error
return(-1);
}
else{
char *proc = argv[1]; //Put procedure into a char array
if(argc == 3){ //for -p,-c,-s, or -m operations
if(argv[2][2] == 'x'){ //If the hexadecimal is a 0x hexadecimal number
firstdec = strtol(argv[2],NULL,0); //do a string to long in base 0
}
else{ //Otherwise if it isn't a 0x hexadecimal
firstdec = strtol(argv[2],NULL,16); //do a string to Long in base 16
}
if(proc[1] == 'p'){ //If the operation is print
printf("Bits: ");
printBits(firstdec); //Call printBits function to print the bits
}
else if(proc[1] == 'c'){
printf("Original : ");
printBits(firstdec); //Print bits of the first hexadecimal
printf("Complement: ");
result = complement(firstdec); //Get complement bits of the first hexadecimal
printBits(result); //Print bits of the result
}
else if(proc[1] == 's'){ //If Find set Bits operation
int setbits = countBits(firstdec); //call countBits function to find how many set bits the number has
printf("%d bits set ",setbits); //Print the number
printBits(firstdec); //Print the bits of the argument
}
else if(proc[1] == 'm'){ //If left shift operation
printf("Original set: ");
printBits(firstdec); //print bits of the argument
printf("Left Shifted: ");
leftShift(firstdec); //Call leftShift function to print the bits of the left shifted
}
}
else if(argc == 4){ //for -u, -i, or -r
if(argv[2][2] == 'x'){ //If the hexadecimal is a 0x hexadecimal number
firstdec = strtol(argv[2],NULL,0); //do a string to long in base 0
}
else{
firstdec = strtol(argv[2],NULL,16); //do a string to long in base 16
}
if(proc[1] != 'r'){
if(argv[3][2] == 'x'){ //If the hexadecimal is a 0x hexadecimal number
seconddec = strtol(argv[3],NULL,0); //do a string to long in base 0
}
else if(argv[3][2] != 'x'){ //If the hexadecimal isn't a 0x hexadecimal number
seconddec = strtol(argv[3],NULL,16); //do a string to long in base 16
}
if(proc[1] == 'i'){ //If intersect operation
result = intersection(firstdec,seconddec);//Place intersection result in result variable
printf("The result is %ld! ",result);
printBits(result); //Print result bits
}
else{ //-u operation
result = bitUnion(firstdec,seconddec); //Find the result of bit union
printf("The result of the union is %ld! ",result);
printBits(result); //print bits of the result
}
}
else{ // rotation operation
int rotateby = strtol(argv[3],NULL,16);//
printf("Rotate bits by %d bit positions: ",rotateby);
set rotated = rrotate(firstdec,rotateby);
printBits(rotated);
}
}
}
return 0;
}
set complement(set num){
return ~num;
}
void printBits(unsigned long i){
int printed = 1;
for (c = 31; c >= 0; c--){
k = i >> c;
if (k & 1){
printf("1");
}
else{
printf("0");
}
printed++;
if(printed == 5){
printf(" ");
printed = 1;
}
}
printf(" ");
}
set bitUnion(set a,set b){
set ans = a | b;
return ans;
}
set intersection(set a,set b){
set ans = a & b;
return ans;
}
int countBits(set a){
int setbits = 0;
int printed = 1;
for (c = 31; c >= 0; c--){
k = a >> c;
if (k & 1){
//printf("1");
setbits++;
}
}
return setbits;
}
set rrotate(set a,int num){
return (a >> num)|(a << (32 - num));
}
//function to shift bits to the left; passes through the set of bits you would like to shift
void leftShift(set a){
int setbits = countBits(a);
int printedbits = 1;
for(i =0;i < setbits;i++){
printf("1");
printedbits++;
if(printedbits == 5){
printf(" ");
printedbits = 1;
}
}
printedbits = 1;
for(j = 0;j <(32-setbits);j++){
printf("0");
printedbits++;
if(printedbits == 5){
printf(" ");
printedbits = 1;
}
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.