typestar

Bitwise in Assembly

and, or, xor, shl: arithmetic below arithmetic.

; and, or, xor, and the shifts: arithmetic below arithmetic.
        global _start

        section .text
_start:
        mov rax, 0b1100
        mov rbx, 0b1010

        mov rcx, rax
        and rcx, rbx            ; 0b1000 = 8
        mov rdx, rax
        or  rdx, rbx            ; 0b1110 = 14
        xor rax, rbx            ; 0b0110 = 6

        shl rax, 2              ; 24
        add rax, rcx            ; 32
        add rax, rdx            ; 46
        mov rdi, rax
        mov rax, 60             ; exit code 46
        syscall

How it works

  1. Binary literals like 0b1100 keep the bits visible.
  2. and masks, or merges, xor differences.
  3. shl rax, 2 multiplies by four in one shift.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
416
Tokens
64
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 70 seconds.

Type this snippet

Step 1 of 4 in Bits & flags, step 15 of 31 in Language basics.

← Previous Next →