In: Computer Science
Port = 0x0055
A) Port &= 0xff0;
B) Port |= 15;
C) Port = Port ^ 0x0005;
D) Port = ( ( Port &~(0x000f) ) | 0x0020 );
Please explain how each logical opperator works and write out the results.
Given:
Port = 0x0055
In decimal : 85
In Binary: 1010101
A)
Port &= 0xff0;
= Port = Port & 0xff0
0xff0 = 4080 in decimal
= 111111110000 in Binary
Port & 0xff0 => 1010101 & 111111110000 = 1010000 in Binary
B)
Port |= 15;
Port = Port | 15
15 = 1111 in Binary
Port | 15 =>
1010101 | 1111 = 1011111 => 95
in decimal
C)
Port = Port ^ 0x0005;
0x0005 => 5 in decimal => 101 in binary
Port ^ 0x0005 = 1010101 ^ 101 => 1010000 => 80 in decimal
D)
Port = ( ( Port &~(0x000f) ) | 0x0020 );
0x000f = 15 in decimal = 1111 in binary
~(0x000f) = -16 in decimal =
11111111111111111111111111110000 in binary
0x0020 = 32 in decimal = 100000 in binary
( Port &~(0x000f) => 1010101 &
11111111111111111111111111110000 = 80 in decimal = 1010000 in
binary
( Port &~(0x000f) | 0x0020 => 1010000 | 100000
=> 1110000 = 112 in decimal