What is a bitmask and a mask?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 17:32:25

问题


On the PHP documentation about JSON it mentions the word bitmask. Wikipedia has it defined as a mask. I don't understand either bitmask or mask or how they are useful. Can some one explain these terms using layman's terms and no jargon?


回答1:


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 internally will be represented 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.

  • Editing particular bits in a byte(s)
  • Checking if particular bit values are present or not.

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:

  • The AND operator extracts a subset of the bits in the state
  • The OR operator sets a subset of the bits in the state
  • The XOR operator toggles a subset of the bits in the state

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!




回答2:


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.

  • To set a bit to 1: mask = mask | (1 << bitIndex)
  • To set a bit to 0: mask = mask & ~(1 << bitIndex)
  • To get a bit (to be able to check it): (mask & (1 << bitIndex)) != 0

All of these operations use the left-shift operator, which moves bits up from least-significant to most-significant positions.




回答3:


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.



来源:https://stackoverflow.com/questions/31575691/what-is-a-bitmask-and-a-mask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!