Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program with 2 functions: Write a void function named bit whose one ar

ID: 3844590 • Letter: W

Question

Write a C program with 2 functions:

Write a void function named bit whose one arg is a pointer to an unsigned. bit's job is

to change that unsigned so that

the low-order byte of the unsigned is changed to all 0-bits

the next byte of the unsigned is unchanged

the next byte of the unsigned is changed to all 1-bits t

he next byte (the high-order byte) of the unsigned has all the bits flipped.

Write a main that sets an unsigned to some value, passes the address of the unsigned to bit, and then outputs the 32 bits of the unsigned, high-order bit first.

Explanation / Answer

To change all bits of low order byte 0, we must AND it with 00000000 but, all other 24bits must remain unchanged, so we need to AND the remaining bits with 1's. So the binary pattern would be 11111111 11111111 11111111 00000000 or 4294967040 in decimal

we need to change 2nd high order byte to 1, so we need to OR 11111111 with those bits, but remaining bits should be 0, so that other parts are not affected, so the binary pattern is 00000000 11111111 00000000 00000000 or 16711680 in decimal.

Flip the highest order byte, we need to XOR those bits with 1, so the binary pattern we get is 11111111 00000000 00000000 00000000 or 4278190080 in decimal.

Exact this methodology is used in the code below

#include<stdio.h>

void bit(unsigned *x) {
   *x = *x & 4294967040;
   *x = *x | 16711680;
   *x = *x ^ 4278190080 ;
}

int main() {
   int i;
   unsigned tmp;
   unsigned input;
   printf("Enter 4byte data in decimal: ");
   scanf("%u", &input);
   bit(&input);
   for (i = 0; i < 32; i++) {
       input = input << 1;
       tmp = input&2147483648; // this extracts the most significat digit
       printf("%d", tmp>>31); //shift it to lowest bit and print
   }
   printf(" ");
}

If you are facing any trouble with the code, please feel free to comment below. I shall be glad to help you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote