Write a program to print out the binary value of a 16 bit number. Create integer
ID: 3649128 • Letter: W
Question
Write a program to print out the binary value of a 16 bit number.Create integers i, count, and mask.
Set 'i' to a hex value of 0x1b53.
Set mask to a value of 0x8000. Why?
print a line to show the hex value of i and then the leader for the binary value like this:
Hex value = 1b53 Binary=
Use a for loop to loop 16 times and print 16 digits, using count as the loop counter
To test for each digit value, bitwise and 'i' with 'mask'
when the result for the bitwise and is true, print the number '1'
when the result for the bitwise and is false, print the number '0'
then shift mask one place to the right
print a new line and then quit
Use prtscrn and make a hard copy of the code with the console output.
Extra: use the modulus of count and print a space after every 4th digit to make the binary easier to read
The output should look like this:
Hex value = 1b53, Binary= 0001 1011 0101 0011
can some one help me finish the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, count, mask;
i=0x1b53; //0001 1011 0101 001
mask= 0x8000;//1000 0000 0000 0000
printf("hex value= 1b53 binary=")
mask = mask>>1;
If((i&mask)
printf("1")
else
print("0")
}
Explanation / Answer
please rate - thanks
in addition to the previous answer, must put the space every 4 places
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int i,count, mask;
i=0x1b53;
mask=0x8000;
printf("hex value=0x1b53 Binary =");
for(count=0; count<15; count=count+1){
if(i & mask) //this compares the bit in the mask with the bit in i. if its a 1 it prints 1, if 0 it prints 0
printf("1");
else
printf("0");
if((count+1)%4==0)
printf(" ");
mask= mask >> 1;
}
getch();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.