Basics, 3 of 8
Operators
Arithmetic, comparisons, logic, the conditional operator, and bitwise operators.
What you’ll build
Two small tools: one that says whether a year is a leap year, and one that stores a file’s
permissions as bits in a single number. Between them they use every kind of operator. FutureBlitz
writes its operators as C does, so &&, !=, and << mean what they mean there.
Arithmetic
+, -, *, and / work as usual, % is the remainder of a division, and ** raises a number
to a power. *, /, and % go before + and -, and parentheses change the order.
Import std.core
Print(2 + 3 * 4, (2 + 3) * 4)
Print(17 / 5, 17 % 5, 17.0 / 5)
Print(2 ** 10, 1.5 ** 2)
Print(-2 ** 2)14 20 3 2 3.4 1024 2.25 4
- before a number binds tighter than **, so -2 ** 2 is (-2) ** 2, which is 4.
Changing a variable
+=, -=, *=, /=, and the others apply an operator to a variable and store the result. ++
adds one, and -- takes one away. All of these are statements: they change a variable, and give
no value, so Local y = x++ is an error.
Import std.core
Local stock = 10
stock *= 3
stock -= 4
stock++
Print(stock)27
Comparisons and logic
==, !=, <, >, <=, and >= compare two values and give a Bool. = is only ever
assignment, so If a = 3 is an error rather than a hidden bug. && is “and”, || is “or”, and
! is “not”. && and || stop as soon as the answer is known: in a && b, b isn’t worked out
when a is False.
Import std.core
Function IsLeap(year: Int): Bool
Return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
End Function
For year In [1900, 2000, 2024, 2025]
Print(year, IsLeap(year))
End For1900 False 2000 True 2024 True 2025 False
A condition must be a Bool. If count Then ... with an Int is an error; write count != 0.
Choosing a value
condition ? a : b gives a when the condition is True, and b when it’s False. It’s for
choosing between two values; an If is for choosing between two pieces of code.
Import std.core
Function IsLeap(year: Int): Bool
Return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
End Function
For year In [2024, 2025]
Local days = IsLeap(year) ? 366 : 365
Print("{year} has {days} days")
End For2024 has 366 days 2025 has 365 days
Bits
The bitwise operators work on the bits of integers: & (and), | (or), ^ (exclusive or), ~
(not), and << and >>, which shift the bits left or right. Each permission here is one bit, so
one number holds all three.
Import std.core
Const READ = 4
Const WRITE = 2
Const RUN = 1
Local mode = READ | WRITE
Print("mode {mode}, in bits {mode:03b}")
Print("can write: {mode & WRITE != 0}")
Print("can run: {mode & RUN != 0}")
mode &= ~WRITE
Print("after taking away write: {mode:03b}")
mode ^= RUN
Print("after flipping run: {mode:03b}")
mode ^= RUN
Print("after flipping run again: {mode:03b}")
Print(1 << 10, 256 >> 4)mode 6, in bits 110 can write: True can run: False after taking away write: 100 after flipping run: 101 after flipping run again: 100 1024 16
~WRITE has every bit but write’s, so mode &= ~WRITE clears that one bit and keeps the rest. ^
flips a bit: mode ^= RUN turns run on when it’s off, and off when it’s on.
Unlike in C, the bitwise operators bind tighter than comparisons, so mode & WRITE != 0 means
(mode & WRITE) != 0, as it reads. {mode:03b} shows the number in binary, three digits wide.
For named sets of flags, a @Flags Enum is clearer still; Enums shows
how.
What you learned
**is a power,%a remainder, and/between integers drops the remainder.+=,++, and the rest are statements, not values.=assigns and==compares; conditions must be Bools.&&and||stop early, and? :chooses between two values.&,|,^,~,<<, and>>work on bits, and bind tighter than comparisons.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.