JavaScript, inside out · Chapter 44 of 51

Bitwise and binary

By the end of this chapter you can see a number as a row of bits: write it in binary or hex, pack several small values into one number, and keep many yes or no answers in a single integer. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Under the hood
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Ravi's rider table has a true or false column for every rule (can deliver, collect cash, work nights, train riders) and gains one every month, all synced to cheap phones over a weak monsoon signal. Kabir wants them in one small number, one bit per permission. Anjali needs the saffron of Tapri's logo, #ff9933, split into red, green and blue for the rider app, and joined back again.

With what you know so far, permissions are an object of booleans that grows a key every month, and #ff9933 is just a string you cannot do sums with. Both jobs need the number underneath, bit by bit.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    toBits and fromBits: numbers to binary strings and back

    Write toBits(n, width). Return the whole number n (0 or more) written in binary, padded on the left with zeros to at least width digits. width defaults to 8: toBits(5) gives '00000101' and toBits(5, 4) gives '0101'. A number that needs more digits is never cut short: toBits(300, 4) gives '100101100'. toBits(0) gives '00000000'.

    Write fromBits(bits). Return the number a binary string stands for, with or without leading zeros: fromBits('00000101') gives 5 and fromBits('0') gives 0.

  2. Task 2

    hexToRgb and rgbToHex: a colour apart and back together

    Write hexToRgb(hex). It takes a colour written '#rrggbb' (six hex digits after the #) and returns an object with three keys, r, g and b, each a number from 0 to 255: hexToRgb('#ff9933') gives { r: 255, g: 153, b: 51 }.

    Write rgbToHex(r, g, b). It joins three channels (each 0 to 255) into a lowercase '#rrggbb' string, always with two digits per channel: rgbToHex(255, 153, 51) gives '#ff9933', rgbToHex(0, 128, 255) gives '#0080ff' and rgbToHex(1, 2, 3) gives '#010203'.

  3. Task 3

    grant, revoke and can: rider permissions as bit flags

    PERMS gives each permission one bit: DELIVER 1, COLLECT_CASH 2, NIGHT_SHIFT 4, TRAIN_RIDERS 8. Keep it as it is.

    Write grant(perms, flag): perms with the flag turned on. Granting a flag that is already on changes nothing: grant(3, PERMS.COLLECT_CASH) gives 3. Write revoke(perms, flag): perms with the flag turned off, and every other bit left alone. Revoking a flag that is off changes nothing: revoke(3, PERMS.NIGHT_SHIFT) gives 3. flag may combine several flags: grant(0, 1 | 4) gives 5 and revoke(15, 1 | 8) gives 6.

    Write can(perms, flags): true when perms has every flag in flags, which may combine several. can(3, PERMS.DELIVER | PERMS.NIGHT_SHIFT) is false, because only one of the two is on. The answer is true or false, not a number.

  4. Task 4

    toggle and listPermissions: flip a flag, name the flags

    Write toggle(perms, flag): perms with the flag flipped, on if it was off and off if it was on. toggle(1, PERMS.NIGHT_SHIFT) gives 5 and toggle(5, PERMS.NIGHT_SHIFT) gives 1. Toggling twice gives back what you started with.

    Write listPermissions(perms): an array of the names of every permission that is on, in the order they appear in PERMS. listPermissions(5) gives ['DELIVER', 'NIGHT_SHIFT'], listPermissions(10) gives ['COLLECT_CASH', 'TRAIN_RIDERS'] and listPermissions(0) gives [].

  5. Task 5 · interview question

    Interview: isPowerOfTwo, without a loop

    Write isPowerOfTwo(n) for a whole number n. Return true when n is a power of two (1, 2, 4, 8, 64, 1024 and so on) and false otherwise: false for 0, for numbers such as 3, 12 and 1023, and for every negative number. The interviewer wants it without a loop: one expression with AND.

What you will learn

  • Other bases: binary and hex literals, toString(2) and parseInt with a radix.
  • Bitwise operators: &, |, ^, ~, << and >>, working on each bit at once.
  • Shifts and masks: several small values packed into one number.
  • Bit flags: one bit per yes or no answer.

More in Under the hood

Build "Bitwise and binary" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.