Write a C program that inverts the bits of a non-negative number provided by the user through argument. Note, the program is to invert the order of the bits, i.e., move the lowest bit to the highest position, 2nd lowest bit to 2nd highest position, etc. It is not to flip the bits (0à1, 1à0). The program prints out the number, its binary representation, the value after the inversion, and the binary representation after the inversion. Your program can assume that the number is a 32-bit unsigned integer (i.e., a number in [0, 232 – 1]).

Respuesta :

Answer:

See explaination for code

Explanation:

Below is the program code.

#include <stdio.h>

#include <stdlib.h>

void toBinary( unsigned int num, int bits[]);

void print(unsigned int num);

unsigned int invert(unsigned int num);

int main(int argc, char *argv[]){

unsigned num, inverted;

if(argc != 2){

printf("ERROR: should pass a number as command line argument\n");

printf("e.g. Usage: %s 4891183\n", argv[0]);

return 1;

}

num = atoi(argv[1]);

inverted = invert(num);

print(num);

print(inverted);

return 0;

}

void toBinary(unsigned int num, int bits[])

{

int i;

//LSB in bits[31]

for(i = 31; i >= 0; i--){

bits[i] = num % 2;

num = num / 2;

}

}

void print(unsigned int num){

int bits[32];

int i = 0;

toBinary(num, bits);

printf("%-20u ", num);

for(i = 0; i < 32; i++){

if(i % 4 == 0)

printf(" "); //space after every 4 bits

printf("%d", bits[i]);

}

printf("\n");

}

unsigned int invert(unsigned int num){

int bits[32];

int i;

toBinary(num, bits);

unsigned int newnum = 0;

for(i = 31; i >= 0; i--){

newnum = (newnum << 1 ) | bits[i];

}

return newnum;

}#include <stdio.h>

#include <stdlib.h>

void toBinary( unsigned int num, int bits[]);

void print(unsigned int num);

unsigned int invert(unsigned int num);

int main(int argc, char *argv[]){

unsigned num, inverted;

if(argc != 2){

printf("ERROR: should pass a number as command line argument\n");

printf("e.g. Usage: %s 4891183\n", argv[0]);

return 1;

}

num = atoi(argv[1]);

inverted = invert(num);

print(num);

print(inverted);

return 0;

}

void toBinary(unsigned int num, int bits[])

{

int i;

//LSB in bits[31]

for(i = 31; i >= 0; i--){

bits[i] = num % 2;

num = num / 2;

}

}

void print(unsigned int num){

int bits[32];

int i = 0;

toBinary(num, bits);

printf("%-20u ", num);

for(i = 0; i < 32; i++){

if(i % 4 == 0)

printf(" "); //space after every 4 bits

printf("%d", bits[i]);

}

printf("\n");

}

unsigned int invert(unsigned int num){

int bits[32];

int i;

toBinary(num, bits);

unsigned int newnum = 0;

for(i = 31; i >= 0; i--){

newnum = (newnum << 1 ) | bits[i];

}

return newnum;

}

In this exercise we have to use the knowledge of computational language in C to describe the code, like this:

We can find the code in the attached image.

The code can be written more simply as:

#include <stdio.h>

#include <stdlib.h>

void toBinary( unsigned int num, int bits[]);

void print(unsigned int num);

unsigned int invert(unsigned int num);

int main(int argc, char *argv[]){

unsigned num, inverted;

if(argc != 2){

printf("ERROR: should pass a number as command line argument\n");

printf("e.g. Usage: %s 4891183\n", argv[0]);

return 1;

}

num = atoi(argv[1]);

inverted = invert(num);

print(num);

print(inverted);

return 0;

}

void toBinary(unsigned int num, int bits[])

{

int i;

//LSB in bits[31]

for(i = 31; i >= 0; i--){

bits[i] = num % 2;

num = num / 2;

}

}

void print(unsigned int num){

int bits[32];

int i = 0;

toBinary(num, bits);

printf("%-20u ", num);

for(i = 0; i < 32; i++){

if(i % 4 == 0)

printf(" "); //space after every 4 bits

printf("%d", bits[i]);

}

printf("\n");

}

unsigned int invert(unsigned int num){

int bits[32];

int i;

toBinary(num, bits);

unsigned int newnum = 0;

for(i = 31; i >= 0; i--){

newnum = (newnum << 1 ) | bits[i];

}

return newnum;

}

See more about C code at brainly.com/question/25870717

Ver imagen lhmarianateixeira