Help for the VSCode editor.
See also Arithmetic Operators in the Go manual. Bitwise operators are considered arithmetic.
Refer to the following logic truth table to help with understanding the answers
a | b | a AND b | a OR b | a XOR b | NOT a | NOT b |
---|---|---|---|---|---|---|
false | false | false | false | false | true | true |
true | false | false | true | true | false | true |
false | true | false | true | true | true | false |
true | true | true | true | false | false | false |
All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:
Running the code fragments
- Right click in Explorer pane to create a new file, e.g.
test.go
- Paste the question code snippet into the editor pane
- Open the terminal window and execute
go run test.go
- Re-use your
test.go
file by replacing the content with that of the next question.
-
Select the correct statements:
- bitwise AND takes two numbers and does OR on every bit of two numbers.
- bitwise OR takes two numbers and does OR on every bit of two numbers.
- bitwise AND takes two numbers and does AND on every bit of two numbers.
- bitwise OR takes two numbers and does AND on every bit of two numbers.
Reveal
B, C
A and D are nonsense!
-
Find the output of the following program:
package main import "fmt" func main() { var x, y int = 100,90 fmt.Println(x & y) fmt.Println(x | y) }
- 90
100 - 64
126 - 0
109 - 64
96
Reveal
64
126To understand how this works, we must first convert both numbers to binary, then apply logic truth table to each column of digits. In binary,
true
is1
andfalse
is 0. Convert the binary result back to decimal.01100100 (100) & 01011010 ( 90) -------- 01000000 ( 64) 01100100 (100) | 01011010 ( 90) -------- 01111110 (126)
- 90
-
Select the correct statements
- The result of XOR is 0 when the two bits are same.
- The result of XOR is 1 when the two bits are opposite.
- The result of XOR is 0 when the two bits are opposite.
- The result of XOR is 1 when the two bits are same.
Reveal
A, B
Refer to above truth table.
-
Find the output of the following program:
package main import "fmt" func main() { var x, y int = 100,90 fmt.Println((x+y) >> 2) }
- 47
- 90
- 83
- 56
Reveal
47
>>
is the bitwise shift right operator. The number of places to shift by in this case is 2. To understand how this works requires a few steps-
Do the addition
x+y
and get the result. 100 + 90 = 190 -
Convert this result to binary:
10111110
-
Now shift right by two columns. Anything that "falls off the end" is discarded. Zeros come in from the left.
Shift 1: `01011111` Shift 2: `00101111`
-
Convert this binary result back to decimal:
00101111
=47
-
Find the output of the following program:
package main import "fmt" func main() { var x, y int = 100,90 fmt.Println(!(((x+y) >> 2 ) == 47)) }
- 47
- error
- true
- false
Reveal
false
There's a lot going on here! To understand how this works requires a few steps. The order we evaluate this expression is determined by the bracketing. We can also tell that the result must be boolean
true
orfalse
, as the final evaluation will be a logical NOT of a boolean comparision using==
so that rules out47
as being a correct answer. There are no syntax errors or anything that would cause a runtime error (like division by zero), soerror
is also not the correct answer.We have to start on the inside and work out.
- The first part is
((x+y) >> 2)
which we did in the previous question, and the result is47
. - The next part is
(((x+y) >> 2 ) == 47)
, i.e.47 == 47
which istrue
- Finally
!(((x+y) >> 2 ) == 47)
, i.e.NOT (47 == 47)
, which isfalse
and is therefore the answer to this question.