Bitmasks. In binary operations, a bitmask can filter bit values using logical operations. For instance, a bitmask of 00001111 one operand of the boolean AND operation, it converts the first four bits of the other operand to 0. The final four bits will be unchanged.
A “Bitmask” is simply a binary number that represents something. 'Mask' means hiding something, The binary representations of 1,2,3,….,7 actually represent a mask. In a particular subset, the i-th element of the set belongs to it if and only if the i-th bit of the mask is set (value is 1).
The MASK command sets or examines the mask register. To search a dump for all words that contain a particular pattern of bits, use the MASK command with the PATTERN and SEARCH commands. The mask register modifies the pattern in the pattern register by masking certain bits in the pattern.
Bitmask also known as mask is a sequence of N -bits that encode the subset of our collection. The element of the mask can be either set or not set (i.e. 0 or 1). This denotes the availability of the chosen element in the bitmask. For example, an element i is available in the subset if the ith bit of mask is set.
Bits and Bytes
In computing, numbers are internally represented in binary. This means, where you use an integer type for a variable, this will actually be represented internally as a summation of zeros and ones.
As you might know, a single bit represents one 0
or one 1
. A concatenation of eight of those bits represent a Byte, e.g. 00000101
, which is the number 5.
I presume you know how numbers are represented in binary, if not, take a look here.
In PHP a number is (mostly) 4 Bytes long. This means that your number actually uses 32 bits of the internal storage. But for simplicity reasons, throughout this answer I will use 8 bit numbers.
Storing states in bits
Now imagine you want to create a program that holds a state, which is based on multiple values that are one(true
) or zero(false
). One could store these values in different variables, may they be booleans or integers. Or instead use a single integer variable and use each bit of its internal 32 bits to represent the different true and falses.
An example:
00000101
. Here the the first bit (reading from right to left) is true, which represents the first variable. The 2nd is false, which represents the 2nd variable. The third true. And so on...
This is a very compact way of storing data and has many usages.
Bit Masking
This is where bit masking comes in. It sounds complex but actually it's very simple.
Bit masking allows you to use operations that work on bit-level.
You actually apply a mask to a value, where in our case the value is our state 00000101
and the mask is again a binary number, which indicates the bits of interest.
By performing binary operations on the mask and the state one could achieve the following:
If we want to set a particular value to true, we could do this by using the OR operator and the following bit mask:
Mask: 10000000b
Value: 00000101b
---- OR ---------
Result: 10000101b
Or one could select a particular value from the state by using the AND operator:
Mask: 00000100b
Value: 00000101b
---- AND ---------
Result: 00000100b
I suggest you to take some deeper look into it and get familiar with the jargon. A good start may be this link.
Goodluck!
It is just a number, as represented in binary. For example, let's say I have 8 boolean values (true
or false
) that I want to store. I could store it as an array of 8 booleans, or I could store it as a single byte (8 bits), each of which store one of the booleans (0
= false
, 1
= true
).
At this point, I can easily manipulate my byte so that I can (1) set specific bits to be on or off (true or false), and (2) check whether specific bits are on or off.
mask = mask | (1 << bitIndex)
mask = mask & ~(1 << bitIndex)
(mask & (1 << bitIndex)) != 0
All of these operations use the left-shift operator, which moves bits up from least-significant to most-significant positions.
In essence, Bitmask is a list of boolean flags (for example isAlive, isMoving, etc) compressed into a single field, usually an integer. It can cut quite a significant amount of JSON string size or memory footprint.
This can be significant especially in PHP where a single boolean in an array can take the same amount of RAM as an integer. There is a very simple Bitmask guide that will explain step by step everything you need to know including how and when to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With