人为什么会死| 尼古拉斯是什么意思| 属龙的今年要注意什么| 半夜十二点是什么时辰| 什么的天空填合适的词| 为什么低烧比高烧可怕| 性转是什么意思| 第一次见家长送什么礼物好| 荷花的花语是什么| 平平仄仄是什么意思| 三伏天吃什么水果好| 吃枸杞对身体有什么好处| 憋屎会造成什么后果| 铁树开花什么意思| 葛根粉有什么功效和作用| 什么食物嘌呤高| 出家当和尚有什么要求| 5月24号是什么星座| 早上起床牙龈出血是什么原因| 信仰是什么意思| hp医学上是什么意思| 鱼石脂是什么| 轴位是什么| 99年属什么生肖| 探花是什么意思| 乙肝吃什么药| 连奕名为什么娶杨若兮| 血压偏低是什么原因| 什么是零售| 子宫肌瘤伴钙化是什么意思| 帅是什么意思| 什么掌不能拍| sds是什么| 肾功能不全是指什么| 肾结石炖什么汤喝最好| 馥是什么意思| 铁锈色痰见于什么病| 拉肚子去医院挂什么科| 输卵管造影什么时候检查最好| 老人吃什么水果好| 胃酸分泌过多是什么原因造成的| 万事大吉是什么意思| 喉咙发炎咳嗽吃什么药好得快| 阿托品是什么| 7月23日什么星座| 扶阳是什么意思| 精液是什么味| 疼痛科主要看什么病| 心脏痛什么原因| 高血压是什么引起的| 1937年是什么年| 慢性结膜炎用什么眼药水| 51年属什么生肖| 胸口闷堵是什么原因| 蓝脸的窦尔敦盗御马是什么歌| 灰指甲挂号挂什么科| 开心果是什么树的果实| 八月十号是什么星座| 升米恩斗米仇什么意思| 花生属于什么类食物| 燃气灶什么牌子好| 属猪的本命佛是什么佛| 蚂蚁为什么会咬人| 脖子发麻是什么原因| 大年初一是什么星座| 步履维艰是什么意思| 男人本色是什么意思| 什么呼什么应| 支气管炎吃什么药效果最好| 什么方法可以让月经快点来| 喉咙干是什么病的前兆| pony什么意思| 容易出汗什么原因| 低压高吃什么药效果好| 梦见捡钱了是什么预兆| 养膘是什么意思| 早上打嗝是什么原因呢| 一字千金是什么生肖| 1997是什么年| 玫瑰花茶和什么搭配好| 韧带是什么样子图片| 骨癌有什么症状有哪些| 尿量少是什么原因| 查宝宝五行八字缺什么| 梦到地震预示什么| 牛肉不能和什么一起吃| x光是检查什么的| 刘邦为什么怕吕后| 舌头紫红色是什么原因| 佳木斯二院全名叫什么| 喉痹是什么意思| 什么人不适合做收银员| 为什么头痛| 1664是什么酒| 四大才子中状元是什么生肖| 克拉是什么单位| 沉鱼落雁闭月羞花是什么意思| 胃手术后吃什么好| spa是什么服务| 千叶豆腐是什么做的| 双重性格是什么意思| 小孩为什么经常流鼻血| 小孩几天不大便是什么原因怎么办| 丙氨酸氨基转移酶是什么意思| 软件开发属于什么行业| 妇科清洁度3度用什么药治疗| 撒丫子是什么意思| 吃醋对身体有什么好处| 去肝火喝什么茶| 十一月三号是什么星座| 青岛为什么叫青岛| 大海里面有什么| 6月25号是什么星座| 北豆腐是什么| 嗓子上火吃什么药| 可乐鸡翅需要什么材料| 24节气分别是什么| 伟岸一般形容什么人| 猪生肠是什么部位| 老化是什么意思| 红烧肉是什么肉| 胸口疼是什么病的前兆| 放下是什么意思| 尿酸高喝什么水最好| 安陵容为什么恨甄嬛| 1990年属马是什么命| 梦到打死蛇是什么意思| 慈字五行属什么| 秦始皇原名叫什么名字| 骨折什么感觉| 南无是什么意思| 草莽是什么意思| 体检生化项目查什么| 为什么会得卵巢癌| 双清是什么意思| 什么是指标到校| 梅花鹿吃什么食物| 狐狸是什么科| 阴茎进入阴道什么感觉| 深柜是什么意思| 尿酸高可以吃什么肉| 否极泰来是什么生肖| 什么是淀粉| 橙色代表什么| 木木耳朵旁是什么字| 微信限额是什么意思| 老舍有什么称号| 结膜炎挂什么科| sample是什么意思| 睾丸皮痒用什么药| 五月四号什么星座| 扎是什么意思| 上海元宵节吃什么| usd什么意思| 7.14什么情人节| 房颤有什么危害| 耳朵烫是什么预兆| 亲子鉴定去医院挂什么科| 什么情况会染上鼠疫| 性腺六项是查什么的| 固本培元什么意思| 不想要孩子用什么办法最好| 尿道口痛什么原因| 脉弦是什么意思和症状| 98年是什么年| 凶猛的动物是什么生肖| 尿酸看什么科| 宫寒吃什么好| 姝字五行属什么的| 肚子里有积水是什么病| 耐受性是什么意思| 托付是什么意思| 狂躁症是什么| 碱是什么| 植物神经功能紊乱吃什么药最好| 7.1什么星座| 红玫瑰的花语是什么| hpv感染有什么症状| pnh是什么病的简称| 蛋白质偏高是什么原因| 荷叶泡水喝有什么作用| 尿频是什么原因引起的| 抗核抗体阳性说明什么| 拉屎的时候拉出血来是什么原因| 结婚5年是什么婚| 慢性肠炎是什么症状| 体育生能报什么专业| 大生化检查都包括什么项目| 为什么趴着睡觉会胀气然后打嗝| 有什么花的名字| 阑是什么意思| 白菜发苦是什么原因| 什么是混合物| 祀是什么意思| 商务专员是做什么的| 早上起床胃疼是什么原因| 乳腺钙化灶是什么意思| 黄色加红色是什么颜色| 心脏早搏吃什么药| 斜视手术有什么后遗症和风险| 泡黄芪水喝有什么好处| 拍黄瓜什么意思| 武松打虎打的是什么虎| 1992年是什么年| 复合维生素b什么时候吃最好| 冠心病喝什么茶最好| 洱海为什么叫洱海| 透亮是什么意思| 莓茶什么人不适合喝| 拿铁和美式有什么区别| 做什么行业最赚钱| 吃惊的什么| 脚趾麻是什么病的前兆| 西米是什么做的| 诺贝尔奖为什么没有数学奖| 怀孕初期吃什么水果好| 七七事变是什么生肖| 男人说做朋友代表什么| 终板炎是什么病| 说三道四的意思是什么| 8.3是什么星座| 91视频是什么| 女上位是什么意思| 鱼翅是什么| 支原体和衣原体有什么区别| 晟这个字读什么| 杏仁和什么不能一起吃| 白细胞多是什么原因| 前列腺钙化是什么意思| 白色裤子配什么上衣好看| 长骨刺是什么原因导致的| 蛋白粉有什么作用| 一直咳嗽吃什么药| 尿胆素1十是什么意思| 减肥有什么好方法| 强调是什么意思| 山东古代叫什么| 甲状腺结节吃什么中药| 咳嗽打什么点滴效果好| 尿急吃什么药| 眼睛有异物感是什么原因| 吃什么提高免疫力和增强体质| 减肥头晕是什么原因| 发炎不能吃什么东西| 懵懂是什么意思| 猫吃什么食物除了猫粮| 1999年五行属什么| 手电筒的金属外壳相当于电路中的什么| 画眉鸟吃什么| 鸡生肖配什么生肖最好| 郭敬明为什么叫小四| 身体逐渐消瘦是什么原因| 被电击后身体会有什么后遗症| 子宫囊肿是什么病| 静若幽兰什么意思| 女人梦见狗是什么预兆| 尚可什么意思| 胶原蛋白是什么东西| 无脑是什么意思| 经常流鼻涕是什么原因引起的| 朗朗原名叫什么| 搬新家有什么讲究和准备的| 槟榔什么味道| 什么药治脂肪肝| 百度Jump to content

易到乐视联合声明:乐视未挪用易到资金 周航恶意诽谤

From Wikipedia, the free encyclopedia
(Redirected from Bit complement)
百度 会前,天津师范大学党委书记荆洪阳会见了各位专家,并就法学学科发展和法学教育与专家进行深入交流。

In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power because of the reduced use of resources.[1]

Bitwise operators

[edit]

In the explanations below, any indication of a bit's position is counted from the right (least significant) side, advancing left. For example, the binary value 0001 (decimal 1) has zeroes at every position but the first (i.e., the rightmost) one.

NOT

[edit]

The bitwise NOT, or bitwise complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:

NOT 0111  (decimal 7)
  = 1000  (decimal 8)
NOT 10101011  (decimal 171)
  = 01010100  (decimal 84)

The result is equal to the two's complement of the value minus one. If two's complement arithmetic is used, then NOT x = -x ? 1.

For unsigned integers, the bitwise complement of a number is the "mirror reflection" of the number across the half-way point of the unsigned integer's range. For example, for 8-bit unsigned integers, NOT x = 255 - x, which can be visualized on a graph as a downward line that effectively "flips" an increasing range from 0 to 255, to a decreasing range from 255 to 0. A simple but illustrative example use is to invert a grayscale image where each pixel is stored as an unsigned integer.

AND

[edit]
Bitwise AND of 4-bit integers

A bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:

    0101 (decimal 5)
AND 0011 (decimal 3)
  = 0001 (decimal 1)

The operation may be used to determine whether a particular bit is set (1) or cleared (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:

    0011 (decimal 3)
AND 0010 (decimal 2)
  = 0010 (decimal 2)

Because the result 0010 is non-zero, we know the second bit in the original pattern was set. This is often called bit masking. (By analogy, the use of masking tape covers, or masks, portions that should not be altered or portions that are not of interest. In this case, the 0 values mask the bits that are not of interest.)

The bitwise AND may be used to clear selected bits (or flags) of a register in which each bit represents an individual Boolean state. This technique is an efficient way to store a number of Boolean values using as little memory as possible.

For example, 0110 (decimal 6) can be considered a set of four flags numbered from right to left, where the first and fourth flags are clear (0), and the second and third flags are set (1). The third flag may be cleared by using a bitwise AND with the pattern that has a zero only in the third bit:

    0110 (decimal 6)
AND 1011 (decimal 11)
  = 0010 (decimal 2)

Because of this property, it becomes easy to check the parity of a binary number by checking the value of the lowest valued bit. Using the example above:

    0110 (decimal 6)
AND 0001 (decimal 1)
  = 0000 (decimal 0)

Because 6 AND 1 is zero, 6 is divisible by two and therefore even.

OR

[edit]
Bitwise OR of 4-bit integers

A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:

   0101 (decimal 5)
OR 0011 (decimal 3)
 = 0111 (decimal 7)

The bitwise OR may be used to set to 1 the selected bits of the register described above. For example, the fourth bit of 0010 (decimal 2) may be set by performing a bitwise OR with the pattern with only the fourth bit set:

   0010 (decimal 2)
OR 1000 (decimal 8)
 = 1010 (decimal 10)

XOR

[edit]
Bitwise XOR of 4-bit integers

A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:

    0101 (decimal 5)
XOR 0011 (decimal 3)
  = 0110 (decimal 6)

The bitwise XOR may be used to invert selected bits in a register (also called toggle or flip). Any bit may be toggled by XORing it with 1. For example, given the bit pattern 0010 (decimal 2) the second and fourth bits may be toggled by a bitwise XOR with a bit pattern containing 1 in the second and fourth positions:

    0010 (decimal 2)
XOR 1010 (decimal 10)
  = 1000 (decimal 8)

This technique may be used to manipulate bit patterns representing sets of Boolean states.

Assembly language programmers and optimizing compilers sometimes use XOR as a short-cut to setting the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures this operation requires fewer clock cycles and less memory than loading a zero value and saving it to the register.

If the set of bit strings of fixed length n (i.e. machine words) is thought of as an n-dimensional vector space over the field , then vector addition corresponds to the bitwise XOR.

Mathematical equivalents

[edit]

Assuming ??, for the non-negative integers, the bitwise operations can be written as follows:

Truth table for all binary logical operators

[edit]

There are 16 possible truth functions of two binary variables; this defines a truth table.

Here is the bitwise equivalent operations of two bits P and Q:

p q F0 NOR1 Xq2 ?p3 ?4 ?q5 XOR6 NAND7 AND8 XNOR9 q10 If/then11 p12 Then/if13 OR14 T15
1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Bitwise
equivalents
0 NOT
(p OR q)
(NOT p)
AND q
NOT
p
p AND
(NOT q)
NOT
q
p XOR q NOT
(p AND q)
p AND q NOT
(p XOR q)
q (NOT p)
OR q
p p OR
(NOT q)
p OR q 1

Bit shifts

[edit]

The bit shifts are sometimes considered bitwise operations, because they treat a value as a series of bits rather than as a numerical quantity. In these operations, the digits are moved, or shifted, to the left or right. Registers in a computer processor have a fixed width, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they determine the values of the shifted-in bits.

Bit addressing

[edit]

If the width of the register (frequently 32 or even 64) is larger than the number of bits (usually 8) of the smallest addressable unit, frequently called byte, the shift operations induce an addressing scheme from the bytes to the bits. Thereby the orientations "left" and "right" are taken from the standard writing of numbers in a place-value notation, such that a left shift increases and a right shift decreases the value of the number ― if the left digits are read first, this makes up a big-endian orientation. Disregarding the boundary effects at both ends of the register, arithmetic and logical shift operations behave the same, and a shift by 8 bit positions transports the bit pattern by 1 byte position in the following way:

Little-endian ordering: a left shift by 8 positions increases the byte address by 1,
a right shift by 8 positions decreases the byte address by 1.
Big-endian ordering: a left shift by 8 positions decreases the byte address by 1,
a right shift by 8 positions increases the byte address by 1.

Arithmetic shift

[edit]
Left arithmetic shift
Right arithmetic shift

In an arithmetic shift (sticky shift), the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the sign bit (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand.

This example uses an 8-bit register, interpreted as two's complement:

   00010111 (decimal +23) LEFT-SHIFT
=  00101110 (decimal +46)
   10010111 (decimal ?105) RIGHT-SHIFT
=  11001011 (decimal ?53)

In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the carry flag), and a new 1 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example:

   00010111 (decimal +23) LEFT-SHIFT-BY-TWO
=  01011100 (decimal +92)

A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to taking the floor of division by 2n. If the binary number is treated as ones' complement, then the same right-shift operation results in division by 2n and rounding toward zero.

Logical shift

[edit]
Left logical shift
Right logical shift

In a logical shift (zero fill shift), zeros are shifted in to replace the discarded bits. Therefore, the logical and arithmetic left-shifts are exactly the same.

However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed two's complement binary numbers.

Circular shift

[edit]

Another form of shift is the circular shift, bitwise rotation or bit rotation.

Rotate

[edit]
Left circular shift or rotate
Right circular shift or rotate

In this operation, sometimes called rotate no carry, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted into the right during a left-shift is whatever value was shifted out on the left, and vice versa for a right-shift operation. This is useful if it is necessary to retain all the existing bits, and is frequently used in digital cryptography.[clarification needed]

Rotate through carry

[edit]
Left rotate through carry
Right rotate through carry

Rotate through carry is a variant of the rotate operation, where the bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag.

A single rotate through carry can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is a logical right-shift, and if the carry flag contains a copy of the sign bit, then x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE is an arithmetic right-shift. For this reason, some microcontrollers such as low end PICs just have rotate and rotate through carry, and don't bother with arithmetic or logical shift instructions.

Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native word size, because if a large number is stored in two registers, the bit that is shifted off one end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.

In high-level languages

[edit]

In C family of languages

[edit]

In C and C++ languages, the logical shift operators are "<<" for left shift and ">>" for right shift. The number of places to shift is given as the second argument to the operator. For example,

x = y << 2;

assigns x the result of shifting y to the left by two bits, which is equivalent to a multiplication by four.

Shifts can result in implementation-defined behavior or undefined behavior, so care must be taken when using them. The result of shifting by a bit count greater than or equal to the word's size is undefined behavior in C and C++.[2][3] Right-shifting a negative value is implementation-defined and not recommended by good coding practice;[4] the result of left-shifting a signed value is undefined if the result cannot be represented in the result type.[2]

In C#, the right-shift is an arithmetic shift when the first operand is an int or long. If the first operand is of type uint or ulong, the right-shift is a logical shift.[5]

Circular shifts
[edit]

The C-family of languages lack a rotate operator (although C++20 provides std::rotl and std::rotr), but one can be synthesized from the shift operators. Care must be taken to ensure the statement is well formed to avoid undefined behavior and timing attacks in software with security requirements.[6] For example, a naive implementation that left-rotates a 32-bit unsigned value x by n positions is simply

uint32_t x = ..., n = ...;
uint32_t y = (x << n) | (x >> (32 - n));

However, a shift by 0 bits results in undefined behavior in the right-hand expression (x >> (32 - n)) because 32 - 0 is 32, and 32 is outside the range 0–31 inclusive. A second try might result in

uint32_t x = ..., n = ...;
uint32_t y = n ? (x << n) | (x >> (32 - n)) : x;

where the shift amount is tested to ensure that it does not introduce undefined behavior. However, the branch adds an additional code path and presents an opportunity for timing analysis and attack, which is often not acceptable in high-integrity software.[6] In addition, the code compiles to multiple machine instructions, which is often less efficient than the processor's native instruction.

To avoid the undefined behavior and branches under GCC and Clang, the following is recommended. The pattern is recognized by many compilers, and the compiler will emit a single rotate instruction:[7][8][9]

uint32_t x = ..., n = ...;
uint32_t y = (x << n) | (x >> (-n & 31));

There are also compiler-specific intrinsics implementing circular shifts, like _rotl8, _rotl16, _rotr8, _rotr16 in Microsoft Visual C++. Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above.[9] GCC does not offer rotate intrinsics. Intel also provides x86 intrinsics.

Java

[edit]

In Java, all integer types are signed, so the "<<" and ">>" operators perform arithmetic shifts. Java adds the operator ">>>" to perform logical right shifts, but since the logical and arithmetic left-shift operations are identical for signed integer, there is no "<<<" operator in Java.

More details of Java shift operators:[10]

  • The operators << (left shift), >> (signed right shift), and >>> (unsigned right shift) are called the shift operators.
  • The type of the shift expression is the promoted type of the left-hand operand. For example, aByte >>> 2 is equivalent to ((int) aByte) >>> 2.
  • If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x1f (0b11111).[11] The shift distance actually used is therefore always in the range 0 to 31, inclusive.
  • If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x3f (0b111111).[11] The shift distance actually used is therefore always in the range 0 to 63, inclusive.
  • The value of n >>> s is n right-shifted s bit positions with zero-extension.
  • In bit and shift operations, the type byte is implicitly converted to int. If the byte value is negative, the highest bit is one, then ones are used to fill up the extra bytes in the int. So byte b1 = -5; int i = b1 | 0x0200; will result in i == -5.

JavaScript

[edit]

JavaScript uses bitwise operations to evaluate each of two or more units place to 1 or 0.[12]

Pascal

[edit]

In Pascal, as well as in all its dialects (such as Object Pascal and Standard Pascal), the logical left and right shift operators are "shl" and "shr", respectively. Even for signed integers, shr behaves like a logical shift, and does not copy the sign bit. The number of places to shift is given as the second argument. For example, the following assigns x the result of shifting y to the left by two bits:

x := y shl 2;

Other

[edit]

Applications

[edit]

Bitwise operations are necessary particularly in lower-level programming such as device drivers, low-level graphics, communications protocol packet assembly, and decoding.

Although machines often have efficient built-in instructions for performing arithmetic and logical operations, all these operations can be performed by combining the bitwise operators and zero-testing in various ways.[13] For example, here is a pseudocode implementation of ancient Egyptian multiplication showing how to multiply two arbitrary integers a and b (a greater than b) using only bitshifts and addition:

c  0
while b  0
    if (b and 1)  0
        c  c + a
    left shift a by 1
    right shift b by 1
return c

Another example is a pseudocode implementation of addition, showing how to calculate a sum of two integers a and b using bitwise operators and zero-testing:

while a  0
    c  b and a
    b  b xor a
    left shift c by 1
    a  c
return b

Boolean algebra

[edit]

Sometimes it is useful to simplify complex expressions made up of bitwise operations, for example when writing compilers. The goal of a compiler is to translate a high-level programming language into the most efficient machine code possible. Boolean algebra is used to simplify complex bitwise expressions.

AND

[edit]
  • x & y = y & x
  • x & (y & z) = (x & y) & z
  • x & 0xFFFF = x[14]
  • x & 0 = 0
  • x & x = x

OR

[edit]
  • x | y = y | x
  • x | (y | z) = (x | y) | z
  • x | 0 = x
  • x | 0xFFFF = 0xFFFF
  • x | x = x

NOT

[edit]
  • ~(~x) = x

XOR

[edit]
  • x ^ y = y ^ x
  • x ^ (y ^ z) = (x ^ y) ^ z
  • x ^ 0 = x
  • x ^ y ^ y = x
  • x ^ x = 0
  • x ^ 0xFFFF = ~x

Additionally, XOR can be composed using the 3 basic operations (AND, OR, NOT)

  • a ^ b = (a | b) & (~a | ~b)
  • a ^ b = (a & ~b) | (~a & b)

Others

[edit]
  • x | (x & y) = x
  • x & (x | y) = x
  • ~(x | y) = ~x & ~y
  • ~(x & y) = ~x | ~y
  • x | (y & z) = (x | y) & (x | z)
  • x & (y | z) = (x & y) | (x & z)
  • x & (y ^ z) = (x & y) ^ (x & z)
  • x + y = (x ^ y) + ((x & y) << 1)
  • x - y = ~(~x + y)

Inverses and solving equations

[edit]

It can be hard to solve for variables in Boolean algebra, because unlike regular algebra, several operations do not have inverses. Operations without inverses lose some of the original data bits when they are performed, and it is not possible to recover this missing information.

  • Has inverse
    • NOT
    • XOR
    • Rotate left
    • Rotate right
  • No inverse
    • AND
    • OR
    • Shift left
    • Shift right

Order of operations

[edit]

Operations at the top of this list are executed first. See the main article for a more complete list.

See also

[edit]

References

[edit]
  1. ^ "CMicrotek Low-power Design Blog". CMicrotek. Retrieved 2025-08-07.
  2. ^ a b JTC1/SC22/WG14 N843 "C programming language", section 6.5.7
  3. ^ "Arithmetic operators - cppreference.com". en.cppreference.com. Retrieved 2025-08-07.
  4. ^ "INT13-C. Use bitwise operators only on unsigned operands". CERT: Secure Coding Standards. Software Engineering Institute, Carnegie Mellon University. Retrieved 2025-08-07.
  5. ^ "Operator (C# Reference)". Microsoft. Retrieved 2025-08-07.
  6. ^ a b "Near constant time rotate that does not violate the standards?". Stack Exchange Network. Retrieved 2025-08-07.
  7. ^ "Poor optimization of portable rotate idiom". GNU GCC Project. Retrieved 2025-08-07.
  8. ^ "Circular rotate that does not violate C/C++ standard?". Intel Developer Forums. Retrieved 2025-08-07.
  9. ^ a b "Constant not propagated into inline assembly, results in "constraint 'I' expects an integer constant expression"". LLVM Project. Retrieved 2025-08-07.
  10. ^ The Java Language Specification, section 15.19. Shift Operators
  11. ^ a b "Chapter 15. Expressions". oracle.com.
  12. ^ "JavaScript Bitwise". W3Schools.com.
  13. ^ "Synthesizing arithmetic operations using bit-shifting tricks". Bisqwit.iki.fi. 2025-08-07. Retrieved 2025-08-07.
  14. ^ Throughout this article, 0xFFFF means that all the bits in your data type need to be set to 1. The exact number of bits depends on the width of the data type.
  15. ^ - is negation here, not subtraction
  16. ^ - is subtraction here, not negation
[edit]
副处级干部是什么级别 四五天不排便是什么原因 血氧低吃什么药效果好 草鱼又叫什么鱼 什么是制片人
什么止疼药见效最快 1202是什么星座 同化是什么意思 guess是什么意思 什么地唱歌
卡介苗是预防什么 脚气是什么菌 芙蓉粉是什么颜色 12月15日是什么星座 曹操属什么生肖
咖啡伴侣是什么东西 嘴唇是紫色的是什么原因 28什么意思 梦字五行属什么 什么上树全靠一张嘴
姌是什么意思bjcbxg.com 花胶是什么hcv8jop4ns7r.cn 莫迪是什么种姓hcv9jop3ns1r.cn 阴道是什么意思hcv8jop2ns7r.cn 加字五行属什么hcv7jop9ns6r.cn
排尿困难是什么原因男性hcv9jop1ns8r.cn 两仪是什么意思hcv9jop5ns5r.cn 冰种翡翠属于什么档次hcv8jop5ns9r.cn 纤维蛋白原是什么意思hcv8jop6ns3r.cn 穴与什么有关hcv9jop3ns3r.cn
宽宽的什么填空hcv9jop0ns6r.cn 脚指麻木是什么病先兆hcv8jop8ns0r.cn 樱桃是什么季节的水果hcv8jop2ns3r.cn 血小板分布宽度低是什么原因hcv8jop7ns7r.cn 胆碱酯酶是什么意思hcv9jop0ns1r.cn
哎是什么意思hcv8jop9ns1r.cn 刑妻克子是什么意思baiqunet.com 诺如病毒是什么症状hcv7jop5ns0r.cn 血便是什么原因引起的hcv9jop0ns9r.cn 巳时是什么时间hcv8jop6ns0r.cn
百度