module Bits {
// BIT CONSTANTS
// use enums to define bit constants
// for more on enums, see the section on enums
enum BitConsts : uint {
A = 1 << 0, // shift 1 left by 0 places
B = 1 << 1,
C = 1 << 2,
D = 0x8, // hex literal
E = C | D,
// use ... to signify that other values are allowed besides those defined, e.g.
// that bit combinations are valid in addition to the specific bits
...
}
fn test() {
let a = 1 << 10; // << is bitwise shift left
let b = 2 >> 1; // >> is bitwise shift right
let x = BitConsts.A | BitConsts.B; // | is bitwise OR
if ((x & BitConsts.A) != 0) { // & is bitwise AND
// ...
}
if (x & ~(BitConsts.A | BitConsts.B)) { // ~ is bitwise NOT
// ...
}
if ((x ^ y) == (BitConsts.A | BitConsts.B)) { // ^ is bitwise XOR
// ...
}
}
}