脊柱侧弯有什么症状| 谷丙转氨酶高吃什么药| 为什么会得脂溢性皮炎| 黄豆炒什么好吃| cin3是什么意思| 央企和国企有什么区别| 十二生肖为什么老鼠排第一| 降维打击是什么意思| 91年的羊是什么命| 亿五行属什么| 排卵期是什么时候| boxing是什么意思| 新生儿囟门什么时候闭合| 车前草的作用是什么| 6月是什么月| 头发油是什么原因| 安阳车牌号是豫什么| 吃银耳有什么功效和作用| 阉割是什么意思| 大马士革是什么意思| 腹部胀疼是什么原因| 胃疼能吃什么水果| 脚踝疼挂什么科| dr检查是什么意思| 小腿浮肿什么原因| 希腊用什么货币| 百年灵手表什么档次| 此加石念什么| 打喷嚏很臭是什么原因| 男生13厘米属于什么水平| 存在感是什么意思| 碳素墨水用什么能洗掉| npv是什么病毒| 头孢不能和什么一起吃| 长口腔溃疡是什么原因| 56岁属什么| ips屏幕是什么意思| 后脑勺白头发多是什么原因| 革兰阴性杆菌是什么| 立春有什么讲究| 牙龈疼痛吃什么药| 公共关系是什么意思| 为什么感冒药吃了想睡觉| 鸡血藤长什么样子图片| 放线菌是什么| 茄子能治什么病| 圆脸女生适合什么发型| 手指甲凹凸不平是什么原因| 秦朝为什么那么快灭亡| 拉肚子是什么原因| 膳是什么意思| 什么是十字花科蔬菜| 梦见女儿哭意味着什么| 猫不能吃什么| 秋葵有什么作用| 稍高回声是什么意思| 男性更年期吃什么药| prich是什么牌子| 阴阳两虚吃什么药最好| none是什么意思| 血小板高有什么危害| 医学ns是什么意思| 宋江是一个什么样的人| 肠胃不好挂什么科| 为什么蛋皮会痒| 组数是什么| 腰疼看什么科| 手脚麻木吃什么药| 无性婚姻会有什么好处| 房早有什么危害| 西字里面加一横是什么字| 检查食道挂什么科| 脾五行属什么| 吃什么对身体好| 肾气不固吃什么中成药| 五蕴指什么| 三月14号是什么星座| 弱水三千什么意思| 头晕恶心呕吐是什么原因| 江苏属于什么方向| gender什么意思| 织锦是什么面料| 草龟吃什么蔬菜| 什么什么言什么| 命运是什么意思| 孩子口臭是什么原因| 虚热是什么意思| kpi是什么意思啊| 施食是什么意思| 慢性咽炎吃什么| 凉白开是什么水| 什么东西补气血效果最好| 梦到大牙掉了一颗是什么意思| 庚子五行属什么| 羟丁酸脱氢酶高是什么原因| 魔性是什么意思| 手脚软无力是什么原因引起的| 头孢是治疗什么病的| 想吃辣是身体缺乏什么| 欢什么喜什么| 炸薯条用什么油| 糖精是什么| 什么药一吃就哑巴了| 茭白不能和什么一起吃| 忙什么呢幽默的回答| 孝顺的真正含义是什么| 白带是什么样的| 宝宝病毒感染吃什么药效果好| 处女座和什么座最配对| 霜降是什么季节| 格林巴利综合症是什么| 祛痣后应注意什么| cathy是什么意思| 肝脏排毒吃什么最好| 胸部ct能检查出什么| 女性真菌感染是什么原因造成的| 尿酸高适合吃什么食物| 金銮殿是什么意思| 下面出血是什么原因| 女生什么时候容易怀孕| rsa胎位是什么意思| 为什么闭眼单脚站不稳| 螺旋体感染是什么意思| 儿童肠胃炎吃什么药| 肠易激综合症什么症状| 物业费都包括什么服务| 红丹是什么| 尿肌酐低是什么原因| 为什么早上起来口苦| 包饺子用什么面粉| 硫酸铜是什么颜色| 优生四项是检查什么| 尿路感染是什么引起的| 母猪上树是什么生肖| 翻江倒海是什么生肖| 人为什么会觉得累| 2006属什么| 氯中毒吃什么可以解毒| 女性腋臭什么年龄消失| 处是什么结构| 1级高血压是什么意思| 湿疹要注意什么| 专一是什么意思| c7是什么意思| 病毒发烧吃什么药| 冬虫夏草是什么东西| 老是头疼是什么原因| 盐酸二甲双胍缓释片什么时候吃| 儿童支气管炎吃什么药| 性功能减退吃什么药| 胃绞痛吃什么药| 柠檬茶喝了有什么好处| 白术是什么样子的图片| 手发麻什么原因| 胎盘下缘达宫颈内口是什么意思| 上眼药是什么意思| 装什么病能容易开病假| 动脉硬化有什么危害| 这是什么虫| 梦见楼塌了是什么意思| 什么含维生素d| 为什么萤火虫会发光| 分开后我会笑着说是什么歌| 属虎什么命| 又双叒叕念什么啥意思| 芒果什么时候成熟| 鹿土念什么| 女人是什么动物| 想起我叫什么了吗| 起鸡皮疙瘩是什么原因| 吃什么补白细胞效果最好| 什么叫粳米| hpv病毒通过什么途径传播| 薄荷泡水喝有什么功效| 光屏是什么| 梦到公鸡是什么意思| 揩油什么意思| 老放屁是什么原因| 什么是沉香| 梦见绿豆是什么意思| 入赘什么意思| 肉是什么结构| 小孩黄疸是什么原因引起的| 丈夫的弟弟叫什么| 红细胞偏低是什么意思| 林檎是什么意思| 血涂片检查什么病| 江西是什么菜系| 戒断反应是什么| beam什么意思| 心梗有什么症状| 二氧化碳分压高说明什么| 夏至是什么意思| 丹字五行属什么| 总胆红素偏高吃什么药| 喉咙发炎咳嗽吃什么药好得快| 一淘是什么| 苏木是什么意思| 柔顺剂有什么用| 测怀孕的试纸叫什么| 老睡不醒是什么原因| 外科和内科有什么区别| 胃怕凉怕冷是什么原因| 梦见玫瑰花是什么预兆| 属兔生什么属相宝宝好| 矢量图是什么格式| 彩虹为什么有七种颜色| c5是什么驾驶证| 孕妇吃鸽子蛋对胎儿有什么好处| 7.11是什么日子| 脚底冰凉是什么原因| 逆光是什么意思| 双侧乳腺腺病是什么意思| 今年30岁属什么生肖| 日光浴是什么意思| 戒烟为什么会长胖| 怀孕血糖高有什么症状| 为什么人死后要盖住脸| 720是什么意思| 母字是什么结构| 李子和什么不能一起吃| 尿酸高吃什么药降尿酸效果好| c5是什么意思| 脸上为什么会长斑| 紫荆花什么时候开| 为什么会得前列腺炎| 阳历7月份是什么星座| 问加一笔是什么字| 人参是什么参| 法西斯是什么意思| 吃小龙虾不能和什么一起吃| 屁股抽筋疼是什么原因| 菩提萨婆诃是什么意思| 什么实实| 猫眼石是什么材质| 脑子里嗡嗡响是什么原因| 什么是理想| 玻璃什么时候传入中国| 神奇的近义词是什么| 发烧拉肚子吃什么药| 记性差是什么原因| 取次是什么意思| 吹空调头疼是什么原因| 生长激素分泌的高峰期是什么时候| 肾水不足是什么意思| 海底有什么生物| 1978年属什么生肖| 什么不可什么| premier是什么牌子| 电销是什么工作| 泉中水是什么生肖| 你姓什么| 肾低密度灶是什么意思| loreal是什么品牌| 肺炎吃什么药有效| 口干舌燥是什么原因| 新鲜感是什么意思| 胆囊大是什么原因| 经略相公是什么官| 间接胆红素是什么| 脱髓鞘疾病是什么病| 陶土色是什么颜色| 狗狗可以吃什么水果| 男士适合戴什么手串| 百度Jump to content

用车|为了安全 这几件事情加油的时候千万别做

From Wikipedia, the free encyclopedia
(Redirected from Bitmask)
百度 只是美中不足的是恒大防守被韩国济州联队三次洞穿大门,球队两场比赛丢掉9个球的表现还是让外界为恒大未来多线作战的前景捏了一把汗。

In computer science, a mask or bitmask is data that is used for bitwise operations, particularly in a bit field. Using a mask, multiple bits in a byte, nibble, word, etc. can be set either on or off, or inverted from on to off (or vice versa) in a single bitwise operation. An additional use of masking involves predication in vector processing, where the bitmask is used to select which element operations in the vector are to be executed (mask bit is enabled) and which are not (mask bit is clear).

Common bitmask functions

[edit]

Masking bits to 1

[edit]

To turn certain bits on, the bitwise OR operation can be used, following the principle that for an individual bit Y, Y OR 1 = 1 and Y OR 0 = Y. Therefore, to make sure a bit is on, OR can be used with a 1. To leave a bit unchanged, OR is used with a 0.

Example: Masking on the higher nibble (bits 4, 5, 6, 7) while leaving the lower nibble (bits 0, 1, 2, 3) unchanged.

    10010101   10100101
 OR 11110000   11110000
  = 11110101   11110101

Masking bits to 0

[edit]

More often in practice, bits are "masked off" (or masked to 0) than "masked on" (or masked to 1). When a bit is ANDed with a 0, the result is always 0, i.e. Y AND 0 = 0. To leave the other bits as they were originally, they can be ANDed with 1 as Y AND 1 = Y

Example: Masking off the higher nibble (bits 4, 5, 6, 7) while leaving the lower nibble (bits 0, 1, 2, 3) unchanged.

    10010101   10100101
AND 00001111   00001111
  = 00000101   00000101

Querying the status of a bit

[edit]

It is possible to use bitmasks to easily check the state of individual bits regardless of the other bits. To do this, turning off all the other bits using the bitwise AND is done as discussed above and the value is compared with 0. If it is equal to 0, then the bit was off, but if the value is any other value, then the bit was on. What makes this convenient is that it is not necessary to figure out what the value actually is, just that it is not 0.

Example: Querying the status of the 4th bit

    10011101   10010101
AND 00001000   00001000
  = 00001000   00000000

Toggling bit values

[edit]

So far the article has covered how to turn bits on and turn bits off, but not both at once. Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently is. This can be achieved using the XOR (exclusive or) operation. XOR returns 1 if and only if an odd number of bits are 1. Therefore, if two corresponding bits are 1, the result will be a 0, but if only one of them is 1, the result will be 1. Therefore inversion of the values of bits is done by XORing them with a 1. If the original bit was 1, it returns 1 XOR 1 = 0. If the original bit was 0 it returns 0 XOR 1 = 1. Also note that XOR masking is bit-safe, meaning that it will not affect unmasked bits because Y XOR 0 = Y, just like an OR.

Example: Toggling bit values

    10011101   10010101
XOR 00001111   11111111
  = 10010010   01101010

To write arbitrary 1s and 0s to a subset of bits, first write 0s to that subset, then set the high bits:

  register = (register & ~bitmask) | value;

Uses of bitmasks

[edit]
A party trick to guess a number from which cards it is printed on uses the bits of the binary representation of the number. In the SVG file, click a card to toggle it.

Arguments to functions

[edit]

In programming languages such as C, bit fields are a useful way to pass a set of named Boolean arguments to a function. For example, in the graphics API OpenGL, there is a command, glClear() which clears the screen or other buffers. It can clear up to four buffers (the color, depth, accumulation, and stencil buffers), so the API authors could have had it take four arguments. But then a call to it would look like

 glClear(1,1,0,0); // This is not how glClear actually works and would make for unstable code.

which is not very descriptive. Instead there are four defined field bits, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_ACCUM_BUFFER_BIT, and GL_STENCIL_BUFFER_BIT and glClear() is declared as

 void glClear(GLbitfield bits);

Then a call to the function looks like this

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Internally, a function taking a bitfield like this can use binary and to extract the individual bits. For example, an implementation of glClear() might look like:

void glClear(GLbitfield bits) {
  if ((bits & GL_COLOR_BUFFER_BIT) != 0) {
    // Clear color buffer.
  }
  if ((bits & GL_DEPTH_BUFFER_BIT) != 0) {
    // Clear depth buffer.
  }
  if ((bits & GL_ACCUM_BUFFER_BIT) != 0) {
    // Clear accumulation buffer.
  }
  if ((bits & GL_STENCIL_BUFFER_BIT) != 0) {
    // Clear stencil buffer.
  }
}

The advantage to this approach is that function argument overhead is decreased. Since the minimum datum size is one byte, separating the options into separate arguments would be wasting seven bits per argument and would occupy more stack space. Instead, functions typically accept one or more 32-bit integers, with up to 32 option bits in each. While elegant, in the simplest implementation this solution is not type-safe. A GLbitfield is simply defined to be an unsigned int, so the compiler would allow a meaningless call to glClear(42) or even glClear(GL_POINTS). In C++ an alternative would be to create a class to encapsulate the set of arguments that glClear could accept and could be cleanly encapsulated in a library.

Inverse masks

[edit]

Masks are used with IP addresses in IP ACLs (Access Control Lists) to specify what should be permitted and denied. To configure IP addresses on interfaces, masks start with 255 and have the large values on the left side: for example, IP address 203.0.113.129 with a 255.255.255.224 mask. Masks for IP ACLs are the reverse: for example, mask 0.0.0.255. This is sometimes called an inverse mask or a wildcard mask. When the value of the mask is broken down into binary (0s and 1s), the results determine which address bits are to be considered in processing the traffic. A 0-bit indicates that the address bit must be considered (exact match); a 1-bit in the mask is ignored.

For example: A network address (traffic that is to be processed): 192.0.2.0 with mask: 0.0.0.255. In binary, the address is 11000000.00000000.00000010.00000000 and the mask is 00000000.00000000.00000000.11111111.

Based on the binary mask, it can be seen that the first three sets (octets) must match the given binary network address exactly (11000000.00000000.00000010). The last set of numbers are ignored by the mask. Thus, all traffic that begins with "192.0.2." are matched. Therefore, with this mask, network addresses 192.0.2.1 through 192.0.2.255 (192.0.2.x) are processed.

Subtracting the normal mask from 255.255.255.255 determines the ACL inverse mask. In this example, the inverse mask is determined for network address 198.51.100.0 with a normal mask of 255.255.255.0:

255.255.255.255 ? 255.255.255.0  = 0.0.0.255 
                  (normal mask) (inverse mask)

ACL equivalents

[edit]

The source/source-wildcard of 0.0.0.0/255.255.255.255 means "any".

The source/wildcard of 198.51.100.2/0.0.0.0 is the same as "host 198.51.100.2"

Image masks

[edit]
Raster graphic sprites (left) and masks (right)

In computer graphics, when a given image is intended to be placed over a background, the transparent areas can be specified through a binary mask.[1] This way, for each intended image there are actually two bitmaps: the actual image, in which the unused areas are given a pixel value with all bits set to 0s, and an additional mask, in which the correspondent image areas are given a pixel value of all bits set to 0s and the surrounding areas a value of all bits set to 1s. In the sample at right, black pixels have the all-zero bits and white pixels have the all-one bits.

At run time, to put the image on the screen over the background, the program first masks the screen pixel's bits with the image mask at the desired coordinates using the bitwise AND operation. This preserves the background pixels of the transparent areas while resets with zeros the bits of the pixels which will be obscured by the overlapped image.

Then, the program renders the image pixel's bits by combining them with the background pixel's bits using the bitwise OR operation. This way, the image pixels are appropriately placed while keeping the background surrounding pixels preserved. The result is a perfect compound of the image over the background.

This technique is used for painting pointing device cursors, in typical 2-D videogames for characters, bullets and so on (the sprites), for GUI icons, and for video titling and other image mixing applications. A faster method is to simply overwrite the background pixels with the foreground pixels if their alpha=1

Although related (due to being used for the same purposes), transparent colors and alpha channels are techniques which do not involve the image pixel mixage by binary masking.

Hash tables

[edit]

To create a hashing function for a hash table, often a function is used that has a large domain. To create an index from the output of the function, a modulo can be taken to reduce the size of the domain to match the size of the array; however, it is often faster on many processors to restrict the size of the hash table to powers of two sizes and use a bitmask instead.

An example of both modulo and masking in C:

#include <stdint.h>
#include <string.h>

int main(void) {
    const uint32_t NUM_BUCKETS = 0xFFFFFFFF;  // 2^32 - 1
    const uint32_t MAX_RECORDS = 1<<10;  // 2^10
    const uint32_t HASH_BITMASK = 0x3FF;  // (2^10)-1

    char **token_array = NULL;
    // Handle memory allocation for token_array…

    char token[] = "some hashable value";
    uint32_t hashed_token = hash_function(token, strlen(token), NUM_BUCKETS);

    // Using modulo
    size_t index = hashed_token % MAX_RECORDS;

    // OR

    // Using bitmask
    size_t index = hashed_token & HASH_BITMASK;

    *(token_array+index) = token;

    // Free the memory from token_array …
    return 0;
}

See also

[edit]

References

[edit]
  1. ^ "Mask R-CNN with OpenCV". PyImageSearch. 2025-08-04. Retrieved 2025-08-04.
什么效应 中二病的意思是什么 ipada1474是什么型号 检查胆囊挂什么科 胆固醇偏高是什么意思
小孩子腿疼是什么原因 颌下淋巴结肿大吃什么药 egg是什么意思 一 什么云 不免是什么意思
仲夏什么意思 纳豆是什么 男人右眼跳是什么预兆 考研是什么时候考 健脾益气是什么意思
光顾是什么意思 升结肠管状腺瘤是什么意思 胃痛吃什么药最有效 什么中药能降血压 腋臭去医院挂什么科
铁蛋白高挂什么科hcv7jop9ns9r.cn 山药跟淮山有什么区别hcv9jop5ns7r.cn 白化病是什么原因引起的hcv9jop3ns0r.cn 什么是正太hcv8jop4ns0r.cn 手足口病用什么药最好hcv8jop4ns5r.cn
为什么医生都不体检hcv7jop9ns5r.cn 中药木香的功效与作用是什么hcv8jop9ns8r.cn 母鸡什么意思hcv8jop8ns7r.cn 孩子肚子疼吃什么药96micro.com 腋毛什么时候开始生长hcv8jop2ns4r.cn
单核细胞百分比偏高是什么意思hcv9jop1ns8r.cn noa是什么意思hcv7jop5ns3r.cn 莞字五行属什么beikeqingting.com 2002年是什么生肖hcv7jop9ns0r.cn se是什么onlinewuye.com
男人右眼皮跳是什么预兆hcv8jop9ns2r.cn 胃肠功能紊乱吃什么药hcv9jop1ns1r.cn 冬练三九夏练三伏是什么意思imcecn.com 头出汗多至头发湿透是什么原因hcv7jop5ns3r.cn 肚脐眼为什么会有臭味cj623037.com
百度