Lab Three
21 Sep 2025
Operators & Expressions (Go Version)
- Rewrite the following expression without using
&&
and not by using conditional statements likeif
.
((i < limit-1 && (c = readChar()) != '\n') && c != 'A')
Hint: Apply De Morgan’s theorem:
- not (A and B) is the same as (not A) or (not B)
- not (A or B) is the same as (not A) and (not B)
- If
x
is1
andy
is2
thenx & y
is zero whilex && y
is1
. Why?
- Write and test a function
rotate
.
The function receives two integer arguments, the value to be rotated (s
) and a count of the number of times to rotate (r
).
Having rotated the value ins
it provides this result as its return value.
func rotate(s uint, r uint) uint
The function must rotate s
to the left r
times, shifting bits from the right (LSB) to the left (MSB).
The test program should prompt for values from the user, call rotate
, and then output both the original value and the shifted value in hexadecimal.
Example:
input two values: 4 2
original: 4
rotated: 10
You should not use a loop in your implementation of rotate
.
Ensure that your implementation is portable.
- Using a single
for
orfor
-while
style statement (with an empty body) and comma-separated operations, write a program that displays 10 powers of 2.
Optional Questions
- Extend the noughts-and-crosses (tic-tac-toe) program to check for the end of the game (i.e., when any row, column, or diagonal is filled with either
X
orO
) and report the result.
- Extend the noughts-and-crosses game so that a single user plays against the computer.
You may develop this program gradually as the course progresses, adding refinements as they occur to you.