Bitmasking Guide

From Uzebox Wiki
Jump to: navigation, search

Hey Everybody. It's day #5 of the summer. I'm still one tutorial behind, but I'll make it up (maybe). So today we're going to learn about bitmasking, and how useful it can be. If you do know how to convert numbers to and from binary great. If not go check out Binary Guide.

So bitmasking is a technique that takles advantage of boleen operators. The three operators are

AND:     &
OR:      |
XOR:     ^

there is also NOT ~ which will inverse the bits of a byte.

Each of the three operators, take two parameters a number before it and a number after it.

AND returns a 1 when both bits are 1, and a zero when one of the numbers is a 0. 11011011 & 10010101 = 10010001


219 & 149 = 145

OR returns a 1 if at least one of the bits is a 1.

Which means

11011011 | 10010101 = 11011111


219 | 149 = 223

XOR returns a 1 if one of the bits is a 1 and one is a 0.

Which means

11011011 ^ 10010101 = 01001110


219 ^ 149 = 78.

~ (Not) simply makes all 0's into 1's and all 1's into 0's.

Join me tomorrow, when I write about practical uses for bitmasking and hexadecimal.