For the questions below write the code using the masks that
Solution
I will use matlab
a) I will use and operator
(xxxx xxxx and 0010 0000) if x=0 then (0 and 1)=0, if x=1 then (1 and 1)=1
0010 0000=32 decimal
if the result is 0010 0000=32 just compare to 32 then bit 5 is 1
if the result is 0 just compare to 32 then bit 5 is 0 (this is for part b)
Matlab code
a=input(\'Input the binary number =\',\'s\') %a binary number example 00101000 save as string
a1=bin2dec(a); % transform the binari to decimal
b1=bin2dec(\'00100000\'); %bit5 to decimal, bit5 =1 to do the and after
result=(bitand(a1,b1)); %do the and operation and transform it to decimal
if (result==32)
display(\'bit5 is 1\');
else
display(\'bit5 is not 1\');
end
B) just the same just invert the condition of the if and the display message
Matlab code
a=input(\'Input the binary number =\',\'s\') %a binary number example 00101000 save as string
a1=bin2dec(a); % transform the binari to decimal
b1=bin2dec(\'00100000\'); %bit5 to decimal, bit5 =1 to do the and after
result=(bitand(a1,b1)); %do the and operation and transform it to decimal
if (result~=32)
display(\'bit5 is 0\');
else
display(\'bit5 is not 0\');
end
c) I will use the and operator as well
(xxxx xxxx and 1000 0000) For bit 7 if the result is 1000 0000 then bit 7 is 1
(xxxx xxxx and 0100 0000) For bit 6 if the result is 0100 0000 then bit 6 is 1
if both are 1 the show the message else show the not messsage
1000 0000=128 and 0100 0000=64
Matlab Code
a=input(\'Input the binary number =\',\'s\'); %a binary number example 00101000 save as string
a1=bin2dec(a); % transform the binari to decimal
b1=bin2dec(\'10000000\'); %bit5 to decimal, bit5 =1 to do the and after
b2=bin2dec(\'01000000\'); %bit5 to decimal, bit5 =1 to do the and after
result1=(bitand(a1,b1)); %do the and operation and transform it to decimal
result2=(bitand(a1,b2)); %do the and operation and transform it to decimal
if ((result1==128)&&(result2==64))
display(\'bit6,7 are 1,1\');
else
display(\'bit6,7 are not 1,1\');
end
D) here i will use the and operator again just i will change the condition of the if for bit 7 to detec the zero, and change the dispaly message
Matlab code
a=input(\'Input the binary number =\',\'s\'); %a binary number example 00101000 save as string
a1=bin2dec(a); % transform the binari to decimal
b1=bin2dec(\'10000000\'); %bit5 to decimal, bit5 =1 to do the and after
b2=bin2dec(\'01000000\'); %bit5 to decimal, bit5 =1 to do the and after
result1=(bitand(a1,b1)); %do the and operation and transform it to decimal
result2=(bitand(a1,b2)); %do the and operation and transform it to decimal
if ((result1~=128)&&(result2==64))
display(\'bit7,6 are 0,1\');
else
display(\'bit7,6 are not 0,1\');
end
E) just change the if conditions of the if for the two of them and he display messages
Matlab Code
a=input(\'Input the binary number =\',\'s\'); %a binary number example 00101000 save as string
a1=bin2dec(a); % transform the binari to decimal
b1=bin2dec(\'10000000\'); %bit5 to decimal, bit5 =1 to do the and after
b2=bin2dec(\'01000000\'); %bit5 to decimal, bit5 =1 to do the and after
result1=(bitand(a1,b1)); %do the and operation and transform it to decimal
result2=(bitand(a1,b2)); %do the and operation and transform it to decimal
if ((result1~=128)&&(result2~=64))
display(\'bit7,6 are 0,0\');
else
display(\'bit7,6 are not 0,0\');
end

