Course outline
Course / The terminal and the language

When a number does not fit

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/08-04-small-box-overflow
cd 08-04-small-box-overflow
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal Fill in three functions that show how int8 and uint8 hold their biggest value, and what happens when you push one past it.

Example If a function were meant to show the same wrap for int16, you would write:

func WrappedInt16() int16 {
	var y int16 = 32767
	y = y + 1
	return y
}

32767 is the biggest int16 can hold. Adding 1 pushes it one past the top, and it wraps around to the smallest value the type can hold.

Rules

  • Edit sizes.go. Each function has a // TODO comment and a return line you will change.
  • Largest should declare an int8 variable holding the biggest value an int8 can hold, and return it. Write it as var x int8 = ... or as x := int8(...): either way the variable must be an int8.
  • Wrapped should declare an int8 variable holding 127, add 1 to it, and return the result.
  • Byte should declare a uint8 variable holding the biggest value a uint8 can hold, and return it.
  • You can write the limit as a number or use math.MaxInt8 / math.MaxUint8 from the lesson.
  • The function names and what they return are fixed. Do not change the lines that start each function.

What you'll practice Picking int8 and uint8, and watching a value silently wrap when it overflows.

Run it

gonomicon fetch week-1/08-04-small-box-overflow
cd 08-04-small-box-overflow
gonomicon test
gonomicon submit

Hints

  1. Largest and Byte just need the biggest value their type can hold — you can write the number directly or use math.MaxInt8 / math.MaxUint8.
  2. Wrapped needs a real int8 variable holding 127, with 1 added to it afterward. The wrap only happens if the addition itself happens inside an int8 box, not a wider one.
  3. var largest int8 = 127 (or math.MaxInt8) and return it. var wrapped int8 = 127; wrapped = wrapped + 1; return wrapped. var b uint8 = 255 (or math.MaxUint8) and return it.

Something wrong with this lesson? Report a problem