ARM Bitwise Logic
Boolean Logic Identities A or 1 = 1 Anything or'd with 1 is 1 A or 0 = A Anything or'd with 0 is that thing A and 1 = A Anything and'd with 1 is that thing A and 0 = 0 Anything and'd with 0 is 0 A xor 1 = not(A) Xoring with 1 flips bit A xor 0 = A Xoring with 0 does nothing
Bitwise Logic Bitwise operations : Work on patterns of bits, not values represented by those bits
Bitwise Logic Bitwise OR:
Binary Immediates #0b starts a binary immediate value No spaces Any 1’s must be in 8 consecutive bits 0b11111111 0b1111111100000000 0b0000000000010101011000 0b100000000000100
ORR ORR: Bitwise OR ORR rd, rs, #___ ORR rd, rs, rm
AND AND: Bitwise AND AND rd, rs, #___ AND rd, rs, rm
EOR EOR: Bitwise exclusive OR EOR rd, rs, #___ EOR rd, rs, rm
Fun Fact In Place Swap EOR can swap two values in place
NOT No NOT instruction – use MVN
BIC BIC: Binary Clear BIC rd, rs, #___ BIC rd, rs, rm Clear bits set in # or rm
Bit Isolation Getting particular bits out of pattern Strategy 1: Shift to wipe out others Want to clear left 8 bits: 1010 1011 1111 0110 1010 1011 1111 0110 Shift left 8 bits: 1111 0110 1010 1011 1111 0110 0000 0000 Shift back right 8 bits: 0000 0000 1111 0110 1010 1011 1111 0110
Bit Isolation Getting particular bits out of pattern Strategy 1: Shift to wipe out others Want to isolate green five bits 1111 1111 1111 0110 1010 1011 1111 0110 Shift left 8 bits 1111 0110 1010 1011 1111 0110 0000 0000 Shift right 27 0000 0000 0000 0000 0000 0000 0001 1110
Bit Isolation Getting particular bits out of pattern Strategy 2: Mask : binary pattern showing bits to keep
Using Masks AND with a 0 guantees 0 AND with 1, keeps other value: 0000 1111 Mask 0011 0000 0101 1011 Data 0000 1011 AND result 0001 0000
Hex Mask F (1111) mask a whole hex digit 0x0000000f Mask 0xfff00000 Data 0x00000008 AND result 0x12300000
Other Masks Sample mask patterns: 0x00 : keep no bits 0x01 : keep bit 0 (0000 0001) 0x04 : keep bit 2 (0000 0100) 0x05 : keep bit 0 & 2 (0000 0101) 0xC0 : keep bit 6-7 (1100 0000)
Bit Isolation Getting particular bits out of pattern Strategy 2: Masking : binary pattern showing bits to keep Want to keep just green bits 1010 1011 1111 0110 1010 1011 1010 0110 Create mask… 0x01F0 or 0b111110000 0000 0000 0000 0000 0000 0001 1111 0000 AND 0000 0000 0000 0000 0000 0001 1010 0000
Mask Samples
High Level Code In C/C++
High Level Code