How does the Bits type work in EK9?
← Getting Started · Ref: Q40
Bits is EK9's dedicated type for bit-level operations — a variable-length ordered collection of bits, NOT an integer. Binary literals: 0b010011. The + operator CONCATENATES bits.
Bitwise: and, or, xor, ~ (not). Shift: << grows (adds zeros), >> shrinks. Integer does NOT have shift operators. Streamable as Booleans (LSB first). Integrates with Colour via .bits() and Colour(bitsValue).
Use 'ek9 -h Bits' to see the full API.
See Q39 for Integer. See Q34 for Colour-to-Bits integration. See Q244 for bitwise operators.
Example
defines module qa.bits defines function booleanToBits() -> b as Boolean <- rtn as Bits: Bits(b) defines program BitsDemo() stdout <- Stdout() // === BITS LITERALS === // Binary literals with 0b prefix a <- 0b010011 b <- 0b101010 stdout.println(`a: ${a}`) stdout.println(`b: ${b}`) // Unset bits unsetBits <- Bits() require ~unsetBits? stdout.println(`Unset isSet: ${unsetBits?}`) // === BITWISE OPERATORS === // and, or, xor anded <- a and b ored <- a or b xored <- a xor b stdout.println(`a and b: ${anded}`) stdout.println(`a or b: ${ored}`) stdout.println(`a xor b: ${xored}`) // not operator (two syntaxes) notted <- ~a alsoNotted <- not a stdout.println(`not a (~): ${notted}`) stdout.println(`not a: ${alsoNotted}`) // === SHIFT OPERATORS === c <- 0b01010011 // Shift left adds zeros on right, grows the bit sequence shiftedLeft <- c << 1 stdout.println(`c << 1: ${shiftedLeft}`) shiftedLeft2 <- c << 2 stdout.println(`c << 2: ${shiftedLeft2}`) // Shift right removes bits from right shiftedRight <- c >> 2 stdout.println(`c >> 2: ${shiftedRight}`) // === CONCATENATION WITH + (NOT ADDITION) === // + joins bits together — this is NOT numeric addition set6 <- 0b010011 set7 <- 0b101010 joined <- set6 + set7 stdout.println(`set6 + set7: ${joined}`) // Append a Boolean (true=1, false=0) withTrue <- set6 + true stdout.println(`set6 + true: ${withTrue}`) withFalse <- set6 + false stdout.println(`set6 + false: ${withFalse}`) // Mutating append growing <- 0b11 growing += true growing += false stdout.println(`Growing: ${growing}`) // === COMPARISON === require a == 0b010011 require a <> b require a < b require b > a // Spaceship ordering <- a <=> b stdout.println(`a <=> b: ${ordering}`) // === LENGTH === stdout.println(`Length of a: ${length a}`) stdout.println(`Length of joined: ${length joined}`) // === STREAMING BITS AS BOOLEANS === // Bits are streamable — each element is a Boolean // Least significant bit first (right to left) set6false <- 0b01001101 partial <- cat set6false | skip 3 | map with booleanToBits | collect as Bits stdout.println(`Skip 3 bits: ${partial}`) // === HASHCODE === hash <- #? a stdout.println(`Hash of a: ${hash}`) // === COPY === copied <- Bits() copied :=: a require copied == a stdout.println(`Copied: ${copied}`)
Common mistakes
E50060 — EK9 Bits does not have setBit() or getBit() methods. Use bitwise operators: and, or, xor, ~, >>, <<. Triggers E50060 — method not resolved. See ek9 -h Bits for the full API.
Incorrect:
shiftedLeft <- c.shiftLeft(1)
Correct:
shiftedLeft <- c << 1
Other ways to ask this
- How do I do bit shifting in EK9?
- What is the difference between Integer and Bits in EK9?
- How do I manipulate individual bits in EK9?
- Why does EK9 have a separate Bits type instead of using integers for bit operations?
Coming from another language?
C/Java/Python/JS: bit ops on integers, signed/unsigned confusion, fixed-width. EK9: dedicated variable-length Bits type, + concatenates, << grows, >> shrinks, streamable as Booleans.
Keywords: start, mask, flag, pixel, and, not, bits, first, binary, migrate, hardware, register, colour, bitwise, shift, concatenate, boolean, intro, or, stream, xor, beginner