1---
2version: 0.1.0
3precision: 4
4---
5# draftply — Bit Flags, Hex & Colors
6# Two everyday uses of binary/hex numbers: file permissions and color codes.
7
8# ── Unix file permissions ───────────────────────
9# Each permission is a bit: read=4, write=2, execute=1 — owner, group, other.
10owner = 4 + 2 + 1 # rwx
11group = 4 + 0 + 1 # r-x
12other = 4 + 0 + 0 # r--
13
14mode = owner * 0o100 + group * 0o10 + other # combine into one octal number
15"Permission mode": oct(mode)
16
17# A umask removes bits: AND with its bitwise complement.
18umask = 0o022
19allowed = band(0o777, bnot(umask))
20"Default mode after umask 022": oct(allowed)
21
22# Does the group have write access? Bit 0o020 is the group-write bit.
23has_group_write = band(mode, 0o020) != 0
24"Group can write?": has_group_write
25
26# ── RGB hex colors ──────────────────────────────
27# A hex color packs three 8-bit channels into one 24-bit number.
28color = 0x3B82F6
29
30r = shr(band(color, 0xFF0000), 16)
31g = shr(band(color, 0x00FF00), 8)
32b = band(color, 0x0000FF)
33
34"Red": r
35"Green": g
36"Blue": b
37
38# Brighten each channel by 20 (clamped to 255) and repack.
39r2 = clamp(r + 20, 0, 255)
40g2 = clamp(g + 20, 0, 255)
41b2 = clamp(b + 20, 0, 255)
42
43brighter = bor(bor(shl(r2, 16), shl(g2, 8)), b2)
44"Brighter color": hex(brighter)
45