Abstract
- A technique used in programming to manipulate individual bits within binary data
AND Bitmasking
1 1 1 0 1 1 0 1 input
(&) 0 0 0 0 1 1 1 1 mask
------------------------------
0 0 0 0 1 1 0 1 output
- The mask position set to
1extracts the bits of the given input, thus extracting a subnet of bits from the input. In the example above, we are extracting the last 4 bits of the input.
MIPS
andiPerform an AND bitmasking operation between a register and an immediate value.
Given
andi $t0, $s0 , 0xF, the mask is0xFwhich1111, so we are getting the last 4 significant bits of$s0.
OR Bitmasking
1 1 1 0 1 1 0 1 input
(|) 0 0 0 0 1 1 1 1 mask
------------------------------
1 1 1 0 1 1 1 1 output
- The mask position set to
1turns the bits of the given input to1. In the example above, we are turning the last 4 bits of the input to1
MIPS
oriPerform an OR bitmasking operation between a register and an immediate value.
Given
ori $t0, $s0 , 0xF, the mask is0xFwhich1111, so we are turning the last 4 significant bits of$s0to1.
MIPS loading 32-bit constant
Why not just use
lwto load the 32-bit constant from memory? Becauselwrequires us to access memory to fetch data, which is slow.Why not just use
li? It is a pseudo-instruction that gets translated to something similar to what’s shown above.
NOR Bitmasking
1 1 1 0 1 1 0 1 input
(nor) 0 0 0 0 0 0 0 0 mask
--------------------------------
0 0 0 1 0 0 1 0 output
MIPS NOR bitmasking
nor $t0, $t0, $zerois used to to perform NOT operation. MIPS doesn’t have a dedicated NOT instruction to keep the instruction set small.
XOR Bitmasking
1 1 1 0 1 1 0 1 input
(^) 0 0 0 0 1 1 1 1 mask
------------------------------
1 1 1 0 0 0 1 0 output
- Flip a subset of bits in the given input. The positions in the mask that are set to
1determine which bits in the input are flipped. The positions in the mask that are set to0will not make any changes to the given input
Using XOR as NOT
We can use XOR as NOT by setting the mask to
1.

/MIPS/assets/mips_load_32_bits_constant.png)