Question: For every part that says "TODO" is a part you need to write. Correctly written code works if every "TODO" is written correctly. #include void print_intbits(int num); int main(int argc, char *argv[]){ int x; printf("x has only the 16th bit set\n"); x = 1 << 16; print_intbits(x); printf("x as decimal: %d\n", x); printf("x as octal : %o\n", x); printf("x as hex : %x\n", x); printf("\n"); printf("set 19th bit as well\n"); x = x | (1 << 19); print_intbits(x); printf("set 3rd bit as well\n"); // TODO: set the indicate bit in x // ??? print_intbits(x); printf("invert the bits\n"); x = ~x; print_intbits(x); printf("clear the 8th bit\n"); x = x & ~(1 << 8); print_intbits(x); printf("clear the 25th bit\n"); // TODO: clear given bit in x // ??? print_intbits(x); printf("clear all bits via xor\n"); x = x ^ x; print_intbits(x); printf("place the pattern 110101 starting at bit 8\n"); x = x | (0b110101 << 8); print_intbits(x); printf("place the pattern 10011111 starting at bit 20\n"); // TODO: clear place given pattern as indicated // ??? print_intbits(x); int y; printf("set y to be x shifted right by 4 bits\n"); y = x >> 4; print_intbits(y); printf("set y to be x shifted right by 24 bits\n"); // TODO: assign y based on shift // ??? print_intbits(y); printf("conditionally print 'x 13th bit SET' or 'x 13th bit CLEAR'\n"); if( x & (1 << 13) ){ printf("x 13th bit SET\n"); } else{ printf("x 13th bit CLEAR\n"); } printf("\n"); printf("conditionally print 'y 4th bit SET' or 'y 4th bit CLEAR'\n"); // TODO: check given bit if(0) { // ??? printf("x 4th bit SET\n"); } else{ printf("x 4th bit CLEAR\n"); } printf("\n"); return 0; } void print_intbits(int num){ char *indexes = "\ 28 24 20 16 12 8 4 0\ "; char bits[32+8+1] = {}; // 32 1/0, 8 spaces, 1 \0 int idx = 0; for(int i=0; i<32; i++){ if(i>0 && i%4==0){ bits[idx] = ' '; idx++; } bits[idx] = num & (1 << (31-i)) ? '1' : '0'; idx++; } printf("bits: %s\n",bits); printf("index: %s\n",indexes); printf("\n"); }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

Question: For every part that says "TODO" is a part you need to write. Correctly written code works if every "TODO" is written correctly.

#include <stdio.h>

void print_intbits(int num);

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

printf("x has only the 16th bit set\n");
x = 1 << 16;
print_intbits(x);

printf("x as decimal: %d\n", x);
printf("x as octal : %o\n", x);
printf("x as hex : %x\n", x);
printf("\n");

printf("set 19th bit as well\n");
x = x | (1 << 19);
print_intbits(x);

printf("set 3rd bit as well\n");
// TODO: set the indicate bit in x
// ???
print_intbits(x);

printf("invert the bits\n");
x = ~x;
print_intbits(x);

printf("clear the 8th bit\n");
x = x & ~(1 << 8);
print_intbits(x);

printf("clear the 25th bit\n");
// TODO: clear given bit in x
// ???
print_intbits(x);

printf("clear all bits via xor\n");
x = x ^ x;
print_intbits(x);

printf("place the pattern 110101 starting at bit 8\n");
x = x | (0b110101 << 8);
print_intbits(x);

printf("place the pattern 10011111 starting at bit 20\n");
// TODO: clear place given pattern as indicated
// ???
print_intbits(x);

int y;
printf("set y to be x shifted right by 4 bits\n");
y = x >> 4;
print_intbits(y);

printf("set y to be x shifted right by 24 bits\n");
// TODO: assign y based on shift
// ???
print_intbits(y);

printf("conditionally print 'x 13th bit SET' or 'x 13th bit CLEAR'\n");
if( x & (1 << 13) ){
printf("x 13th bit SET\n");
}
else{
printf("x 13th bit CLEAR\n");
}
printf("\n");

printf("conditionally print 'y 4th bit SET' or 'y 4th bit CLEAR'\n");
// TODO: check given bit
if(0) { // ???
printf("x 4th bit SET\n");
}
else{
printf("x 4th bit CLEAR\n");
}
printf("\n");

return 0;
}

void print_intbits(int num){
char *indexes = "\
28 24 20 16 12 8 4 0\
";
char bits[32+8+1] = {}; // 32 1/0, 8 spaces, 1 \0
int idx = 0;
for(int i=0; i<32; i++){
if(i>0 && i%4==0){
bits[idx] = ' ';
idx++;
}
bits[idx] = num & (1 << (31-i)) ? '1' : '0';
idx++;
}
printf("bits: %s\n",bits);
printf("index: %s\n",indexes);
printf("\n");
}

Expert Solution
steps

Step by step

Solved in 3 steps with 6 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,