Please explain the reasoning behind these solutions to me. This is written in C.
ID: 3747449 • Letter: P
Question
Please explain the reasoning behind these solutions to me. This is written in C.
Laboratory Worksheet #02 Logic Exercise Answer the following questions given: char a,b,c,d a 0x01 b 0x00 c0x00 a-0xFF 0x09 What is the value of d after execution of each of the following lines? 0x00 0x09 0x04 0x0C 0x00 3) d b &0x04 4) d = b & -0x33; Are the following TRUE or FALSE? 6) (a &&c) 7) (b && c) 8) (b &&0x04) false true Also answer the following: What is the value of d after execution of each of the following lines? 10) d b l 0x10; 11) d= b I -a ; 12) d= b 1 1a; 0x09 0x1F OxFF 0xOF Are the following TRUE or FALSE? 13) (a l c 14) (a I (c&0x10)) 15) (a I Ib) false false When complete, insert Worksheet 2 in your laboratory notebook. Worksheets are required when notebooks are graded.Explanation / Answer
Given,
a = 0x00 = 0000 0000 in binary
b = 0x0F = 0000 1111 in binary
c = 0x09 = 0000 1001 in binary
~a = 0xFF = 1111 1111 in binary
!a = 0x01 = 0000 0001 in binary
!b = 0x00 = 0000 0000 in binary
!c = 0x00 = 0000 0000 in binary
1) d = a & c
& is a bitwise AND operator
a & c = ( 0000 0000 ) & (0000 1001 )
= 0000 0000 which is result of bitwise AND as (1 & 0 =0 ) and (1&1 = 1 )
= 0x00 in hex
2) d = b & c
b & c = (0000 1111 ) & (0000 1001)
= 0000 1001
= 0x09 in hex
3) d = b & 0x04
b & 0x04 = (0000 1111 ) & (0000 0100)
= ( 0000 0100)
= 0x04
4) d = b & ~0x33
~ is a negation operator. It flips 1 to 0 and 0 to 1
~0x33 = ~(0011 0011)
= 1100 1100
b & (1100 1100) = (0000 1111) & ( 1100 1100)
= (0000 1100)
= 0x0C
5) d = b & !c
b & !c = (0000 1111) & (0000 0000 )
= 0000 0000
= 0x00
6) (a && c)
&& is a logical AND operator. It returns true if both a and b are true else return false.
Both negative and positive values are considered as true.
Here, a = 0x00 which false
c = 0x09 which is true i.e 1
(a && c ) = (false && true) = false