We start by understanding binary numbers. Pretty much everybody understands
binary numbers, right? They started teaching that stuff in grade school
about the time I got out of college. Here is counting from zero to twenty
in decimal (base ten), binary (base two), and hexadecimal (base 16):

Dec Bin Hex Power0 0000 01 0001 1 2^{0}2 0010 2 2^{1}3 0011 34 0100 4 2^{2}5 0101 56 0110 67 0111 78 1000 8 2^{3}9 1001 910 1010 A11 1011 B12 1100 C13 1101 D14 1110 E15 1111 F16 10000 10 2^{4}17 10001 1118 10010 1219 10011 1320 10100 14

I listed the hexadecimal next to the binary because each four binary
digits (called *bits*) corresponds to exactly one hex digit. When
the binary spills over into a fifth digit, the hex spills over into a second
digit. This is important because if you want to spell a binary number in
your program, you normally use hex, so that (for example)

(binary)where the "1101 1110 1010 1101 1011 1110 1110 1111 = 0xDEADBEEF(hexadecimal)

As you know, each binary digit, each

Notice how each bit in the binary number represents a particular power of two, 2 times itself that many times (where

Now, as long as there isn't something already in that bit position,
you can turn it on by adding a number with just that bit on, and every
other bit off. However, if it's already set, that starts to scramble other
bits in the number, so we have a couple of operators that operate only
on their corresponding bits, with no carries out into adjacent bits. To
turn a bit on, we use the *logical OR* ("`|`")
and to turn it off we use *logical AND* ("`&`").
It's also useful to flip one or more bits from one to zero or zero to one,
which is called *exclusive OR* ("`^`").
All of these (including the shifts) are called *bitwise* operators.
You could write a little program to try these out and see what they do,
but that will be easier after the next couple of lessons. So for now you
get tables, like the times table you memorized in grade school -- or maybe
they don't do that any more, y'all have calculators in your phones...

& | 0 1 | | 0 1 ^ | 0 1---+------ ---+------ ---+------0 | 0 0 0 | 0 1 0 | 0 11 | 0 1 1 | 1 1 1 | 1 0

Before we leave arithmetic and assignments, I should mention two
useful shortcuts that show up everywhere, the *increment* (`++`)
and
*decrement* (`--`) operators. In a program where variable
`i`
is an integer, "i++;" is syntactic sugar for (means exactly the same as)
"`i=i+1;`" and "`i--;`" means the same as "`i=i-1;`".
You may also see the operator before the variable "`++i;`" which
is the same, except when used inside an expression value. Java allows any
assignment statement to be used as a value, but I do not recommend it.
My college-level students sometimes got that wrong, and you don't need
that kind of problem in your programs.

Revised: 2021 August 28