o Invoke the function display_binary () to display the o Declare a mask for unit
ID: 3701732 • Letter: O
Question
o Invoke the function display_binary () to display the o Declare a mask for unit number - the lowest 3 bits in . Use this mask and bitwise & to retrieve the unit code in binary the code. number - store it in a variable. Display the unit number to the screen. Declare a mask to retrieve the signal (the 7th bit) . Use thismask and bitwise & to retrieve the signal - Display the outcome to the screen. o Declare a mask to retrieve the number of tanks. . Use the mask, bitwise & and then >> to retrieve the number of tanks. Display it to the screen. o Declare a mask to change the safety signal, bit 7, to 0 and leave all the other bits in the code the same. Use this mask and bitwise to change the code. . Display the changed code in decimal and binaryExplanation / Answer
It was not clear what does the question mean by "number of tanks" so not answering that part. The code is able to handle 32 integers including negative integers.
#include <stdio.h>
void display_binary(unsigned int code,int flag)
{
if(flag == -1) printf("1");
unsigned int i;
for (i = 1 << 31; i > 0; i>>=1)
{
if(code & i)
{
printf("1");
}
else
{
printf("0");
}
}
}
int main(int argc, char ** argv)
{
int code,flag=1;
printf("Enter Code: ");
scanf("%d",&code);
if(code<0) {
flag=-1;
code*=-1;
} // Handling negative numbers
code = (unsigned int)(code);
// Displaying code in binary
printf("Binary Representation : ");
display_binary(code,flag);
printf(" ");
// Mask to get the last three bits
unsigned int three_bit_mask = 1 + (1<<1) + (1<<2) ;
unsigned int unit_number = (code & three_bit_mask);
printf("Unit Number : %d ",unit_number);
// Mask to get the seventh bit
unsigned int seventh_bit_mask = 1<<6 ;
code & seventh_bit_mask ? printf("Seventh Bit : 1 ") : printf("Seventh Bit : 0 ");
// Mask to change seventh bit to zero
int change_seventh_mask = 0;
int i;
for(i=0;i<=31;i++)
{
if(i!=6)
change_seventh_mask += (1<<i);
}
// Printing the chaged code value
unsigned int changed_code = (code & change_seventh_mask);
printf("Changed Code : %d ",flag*changed_code);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.