stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 275

Day 275 of 365 · Expert · Month 10: Bits and data

Bitwise: and, or, xor, nor, not

and, or, xor and nor work on every bit separately: set, clear, test and toggle bits with masks, plus not and why 0 becomes −1 with it.

Lesson 16 min Week 40

What you learn today

  • Set, clear and test bits
  • Masks
  • not and negative numbers

Yesterday you saw how many bits a number holds. Today you set, clear and test them one by one. You have known the instructions since week 4: and, or, xor and nor. Back then you used them with 0 and 1. In fact they work on every bit of a number separately.

Bit by bit

Instructionbit in the result is 1 if …
andboth bits are 1
orat least one is 1
xorexactly one is 1
norboth are 0
notthe bit was 0 (single input)
and r0 %1100 %1010   # %1000 = 8
or r1 %1100 %1010    # %1110 = 14
xor r2 %1100 %1010   # %0110 = 6

Masks

A mask is a number whose 1 bits say which places you care about. That gives four moves:

GoalInstructionExample for bit 2
set a bitor with the maskor r0 r0 %100
clear a bitand with the inverted masknot m %100, then and r0 r0 m
test a bitand with the mask, then snezand r1 r0 %100
toggle a bitxor with the maskxor r0 r0 %100

not and negative numbers

not flips all bits. 0 does not become 1 but −1, because “all bits 1” means −1 for signed integers. 5 becomes −6. That is not a mistake: not x is always −x − 1.

not is handy for clearing masks. not mask %100 returns a number in which only bit 2 is zero. and with it clears exactly this bit, however long the number is:

not mask %100         # all bits 1 except bit 2
and value value mask    # bit 2 cleared

By the way, nor x 0 is the same as not x – both inputs 0 means 1 for nor.

Warning

Logical and bitwise and are the same instruction. and r0 2 1 gives 0 although both values are “true”: 2 is %10, 1 is %01, no bit in common. With pure 0/1 values and behaves logically, with other numbers it does not. So turn truth values into 0 or 1 first with snez.

Note

Bit instructions convert decimals into integers first. 5.7 becomes 5 – the fractional part is dropped.

Diagram

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
For bit instructions the decimal is turned into a 64-bit integer, combined bit by bit and converted back.

Exercise

Task

value (r0) holds a status word. Compute with bit instructions:

  • set = value with bit 2 set
  • cleared = value with bit 0 cleared (build the mask with not)
  • tested = 1 if bit 4 is set, otherwise 0
  • flipped = value with bit 1 toggled
  • inverted = all bits of value flipped

Hints

Hint 1

Set with or, toggle with xor, invert with not.

Hint 2

Clear: not mask 1, then and cleared value mask.

Hint 3

Test: and with %1_0000, then snez.

Quiz

5 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere