cint

cint university / 101 / ced, the cint editor

101-04: numbers have sizes

This is lesson 101-04 of CU-101.2, checked against cint 0.2.0.

You will meet cint's eight kinds of whole number, see how large each can be and watch cint refuse a number that does not fit.

the program

U8 brightness = 255;
I8 temperature = -40;
I32 population = 2_000_000_000;
I64 grains = 9_223_372_036_854_775_807;
"brightness {brightness}, smallest U8 {U8.min}, largest {U8.max}\n";
"temperature {temperature}, I8 runs from {I8.min} to {I8.max}\n";
"population {population}, I32 tops out at {I32.max}\n";
"grains {grains}\n";

Run this program in ced

Run it:

brightness 255, smallest U8 0, largest 255
temperature -40, I8 runs from -128 to 127
population 2000000000, I32 tops out at 2147483647
grains 9223372036854775807

what happened

A computer keeps a number in a box of bits. A bit is a single 0 or 1. Eight bits can be set in 256 different ways, so a box of 8 bits can hold 256 different numbers. A box of 16 bits can hold 65,536 different numbers, and every extra bit doubles the count.

The name of a type says the size of its box. The kind of number a variable holds is called its type. In cint, a type's name says two things: U for numbers that are never negative (unsigned), or I for numbers that may be negative (signed: an integer with a sign), then the number of bits.

Type Smallest Largest
U8 0 255
U16 0 65535
U32 0 4294967295
U64 0 18446744073709551615
I8 -128 127
I16 -32768 32767
I32 -2147483648 2147483647
I64 -9223372036854775808 9223372036854775807

You never need to learn these by heart: U8.min and U8.max give them, as the program shows, and the same works for every type.

Pick the type that fits the job. A pixel's brightness runs from 0 to 255, so it fits a U8, and a picture with millions of pixels takes a quarter of the memory it would as I32. When in doubt, use I64: it holds any whole number from about minus nine billion billion to about nine billion billion.

Underscores make long numbers readable. 2_000_000_000 is two billion. cint ignores the _; it is there for your eyes.

cint checks that a number fits its box. Write a number that cannot fit, and cint refuses the program before it runs. This lesson's mistake shows it.

cint never moves a number to another type on its own. Even when the number would fit, you say so:

U8 b = 200;
I64 wide = b as I64;
"{wide}\n";

Run this program in ced

This prints 200. Without as I64, cint refuses the line. as is a checked conversion: cint checks that the number fits the new type, and lesson 101-05 shows what happens when it does not. Keeping each number in the type you chose means you always know which box it is in.

change it

  1. Print the largest U16.
  2. Put -1 in a U8. What does cint say?
  3. Make a U8 that holds 200 and add it to an I64 that holds 100, using as. The answer, 300, would not fit in a U8.
one way to write the third change
U8 a = 200;
I64 total = (a as I64) + 100;
"{total}\n";

Run this program in ced

This prints `300`.

when it goes wrong

U8 brightness = 256;

Run this program in ced

error[C2003]: literal does not fit its type
  --> brightness.ci:1:17
   |
 1 | U8 brightness = 256;
   |                 ^
   |

A literal is a value written out in the program, like 256 or text in quotes. The largest U8 is 255, so 256 does not fit, and cint says so before anything runs. Change the type to U16, or the number to 255.

new words

bit, type, unsigned, signed, literal, conversion

Next: lesson 101-05, when a number runs out.