Syntax
Computes on the bits of whole numbers: && and, || or, ~~ exclusive or, ! flips every bit, << and >> shift.
<a> && <b>
<a> || <b>
<a> ~~ <b>
!<a>
<a> << <n>
<a> >> <n>Also written : &&, ||, ~~, !, <<, >>
Parts
| <a> | A number, worked on as a signed 32-bit integer. Decimals are dropped: 3.7 || 0 is 3. |
|---|---|
| <b> | A number, worked on as a signed 32-bit integer. |
| <n> | The number of bit positions to shift. >> keeps the sign: -16 >> 2 is -4. |
Good to know
- The symbols are bitwise and the words are logical.
&&is notAND, and||is notOR. This is the reverse of C and JavaScript. - A bit test needs parentheses:
&&comes after=, soflags && 4 = 4meansflags && (4 = 4). Write(flags && 4) = 4, or simplyIF flags && 4 THEN. - Results are signed 32-bit:
1 << 31is -2147483648, and!5is -6. XORdoes not exist: it compiles, but the program does not start. Write~~.- A single
&is not string concatenation:"a" & "b"gives 0. Join strings with+. - Inside a
#IFcondition,&&,||and!are the logical words, not these operators. - Binary literals are written
%1010, hexadecimal0xFF. Flags fromENUMERATIONBINARYcombine with||.
Example
flags = %1100Console(Str(flags && %1010)) ' 8Console(Str(flags || %0011)) ' 15Console(Str(flags ~~ %1010)) ' 6Console(Str(!0)) ' -1Console(Str(1 << 4)) ' 16Console(Str(-16 >> 2)) ' -4IF (flags && 4) = 4 THEN Console("bit set") ' bit setIn the manual : Constants and operators