1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sattari [20]
4 years ago
6

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]).
Computers and Technology
1 answer:
denpristay [2]4 years ago
7 0

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;

}

You might be interested in
The internet has provided great opportunities for fundraising. true/false
alekssr [168]
True, sites such as kickstarter and just giving are fantastic examples of such.
8 0
3 years ago
How will 5G technology affect the current classroom? What are some concerns about 5G technology? Is 5G technology worth the risk
notsponge [240]

Answer:Death is a risk of 5g towers

Explanation:Someone caught a video of birds falling dead out of the sky because of the radiation from the 5g towers. There is enough radiation to make your eyes melt before you get close to it.

4 0
4 years ago
True or false
ICE Princess25 [194]
1. True
2. Usually true, but it depends on the search engine you're using.  For example, Google lets you search for several words without commas.
3 0
3 years ago
Most wheel/tire assemblies are balanced using lead weights which should be removed and segregated from other metals
crimeas [40]
The answer is true. I took the test.
6 0
3 years ago
Read 2 more answers
In choosing an off-the-shelf software solution and the supported task is generic, which type of software package would you use
Margaret [11]

The software package that I would use is packaged software producer. They are defined as potential sources of application software.

<h3>Software</h3>

A software can be defined as a series of sequential instructions that indicate a computer what to do.

Packaged software producers are defined as potential sources of application software.

The industries develop these softwares to run them on different types of platforms.

Learn more about software here:

brainly.com/question/1022352

5 0
2 years ago
Other questions:
  • Write a converter program for temperatures. This program should prompt the user for a temperature in Celsius. It should then con
    10·1 answer
  • Based on the passage​ and/or drawing on your prior​ knowledge, you realize that an HMO is​ what?
    9·1 answer
  • What specific type of tools can assist teams by identifying attacks and indicators of compromise by collecting, aggregating, and
    8·1 answer
  • After the following code runs, what will be the value of result? var x = 30; function get () { return x; } function set (value)
    10·1 answer
  • Suppose that a computer can run an algorithm on a problem of size 1,024 in time t. We do not know the complexity of the algorith
    14·1 answer
  • Whats the answer to this question?
    7·1 answer
  • Suppose that we have a set of activities to schedule among a large number of lecture halls, where any activity can take place in
    12·1 answer
  • Write if true or false
    12·1 answer
  • Xbox one is not turning on and power brick is orange why is that?​
    6·1 answer
  • a) pencil b) ink bottle c) polygon d) eye 3. Under which of the following can an operating system be classified? b) software c)
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!