闭关修炼是什么意思| 脑疝是什么意思| 做美甲师容易得什么病| 二月二十三日是什么星座| 左眼老是跳是什么原因| 米线是用什么做的| 二十年婚姻是什么婚| 心脏搭桥和支架有什么区别| 衣原体感染是什么意思| 6月28号是什么星座| 颌下淋巴结肿大挂什么科| 梦见病人好了什么预兆| 身上臭是什么原因| 乐极生悲是什么意思| 家有一老如有一宝是什么意思| 古代广东叫什么| 晚上喝蜂蜜水有什么好处| 吃维生素c和维生素e有什么好处| yjs是什么意思| 毒瘤是什么意思| 么么什么意思| 带状疱疹吃什么药好| 角膜塑形镜什么牌子好| 苍蝇是什么味道| 皮肤长痘痘是什么原因| 胃在什么位置图片| 局灶是什么意思| 什么网站可以看三级片| 舒筋健腰丸为什么这么贵| 蓝蓝的天上白云飘是什么歌| 立秋抓秋膘吃什么| 什么手串最好| 嘴里有粘液是什么原因| 吃什么解油腻| 青光眼是什么原因引起的| 睡不着挂什么科| 黄药是什么| 浇去掉三点水读什么| 右小腿抽筋是什么原因| 以梦为马是什么意思| 吃什么受孕率又快又高| 怀孕了用排卵试纸测会显示什么| 司法鉴定是干什么的| 不将就什么意思| 尿比重是什么意思| 定坤丹什么时候吃最好| 草字头加个弓念什么| 航班号是什么| 甲状腺球蛋白抗体低说明什么| 止汗药什么最好| 世界上最大的鸟是什么鸟| 丰富的近义词和反义词是什么| 威图手机为什么那么贵| 吃什么食物可以降低胆固醇| 什么时间吃苹果最好| balenciaga什么品牌| 布洛芬治什么| 福祸相依什么意思| 藩台是什么官| 洋芋是什么| 牛油果什么时候吃最好| 挂号特需是什么意思| 幽门螺杆菌是什么意思| 重色轻友是什么意思| 湿疹是什么原因造成的| 菊花的功效是什么| 腺肌症吃什么药| 麂皮是什么皮| 异淋是什么意思| 孩子总爱哭是什么原因| 结肠炎吃什么药效果最好| 特殊情况是什么意思| 昱读什么| 有黄痰吃什么药| 吃什么对胃好养胃| 7月29日什么星座| 梦见女婴儿是什么意思| 时光荏苒岁月如梭是什么意思| 骨客念什么| 咳嗽有痰吃什么药效果好| 牛肉和什么蔬菜搭配好| 印泥干了用什么稀释| 女生的下面叫什么| 户籍所在地是指什么| 中药木香的功效与作用是什么| 什么中生什么| 袋鼠吃什么食物| 湿气重能吃什么水果| 火疖子是什么| 什么是思维| 月子早餐吃什么好| 淋巴细胞高是什么意思| 老打喷嚏是什么原因| 赤诚相见是什么意思| 豌豆的什么不能吃| 人巨细胞病毒是什么病| 猝死是什么原因造成的| 例假是什么意思| 有什么蔬菜| 养胃吃什么好| 反响是什么意思| 乙肝三项检查什么| 复制是什么意思| 晕倒挂什么科| 花生为什么叫落花生| 医院脱毛挂什么科| h型高血压是什么意思| camp是什么| 甲亢病是什么原因引起的| 文胸是什么意思| 吃三七粉有什么效果| 男人都是大猪蹄子是什么意思| 产成品是什么意思| 举重若轻什么意思| 人流挂什么科| 码是什么单位| 送礼送什么好| 查乳房挂什么科| 过敏性鼻炎挂什么科| 黄花菜都凉了什么意思| 火车票无座是什么意思| 为什么作什么的成语| 心脏难受是什么原因| 不忘初心方得始终是什么意思| 考护师需要什么条件| 什么原因不来月经| 芒果吃多了有什么坏处| 包皮炎吃什么消炎药| 肝郁气滞吃什么药| 怀孕期间吃什么对胎儿发育好| 五阴是什么| 中元节不能穿什么衣服| 甲功不正常会得什么病| 女人是什么| 益生菌什么时候吃| 左下腹有什么器官| fb是什么意思| 女性尿路感染吃什么药好得快| 女性性高潮是什么感觉| 入伏吃什么| 肺阳虚吃什么中成药| 什么雨| 香椿是什么| 爽是什么结构| 可可粉是什么| 什么是冷血动物| 蒙羞是什么意思| 坐飞机什么不能带| 小孩吃什么能长高| 三月三十号是什么星座| 血脂高吃什么药好| 祸不单行什么意思| 祛湿气喝什么茶| 祛风是什么意思| 冰粉是用什么做的| 打蛋白针有什么作用| 火烈鸟吃什么| 涵字取名的寓意是什么| 腿弯疼是什么原因| 什么原因引起荨麻疹| 左旋肉碱是什么东西| 天丝是什么成分| 甲沟炎涂什么药膏| 佝偻病是什么| 活动性肺结核是什么意思| 人流后吃什么水果| 什么是宫寒| 充电宝充电慢是什么原因| 青蒿素是什么| 胸闷气短吃什么特效药| 玫瑰糠疹用什么药| 胃镜是什么| 惟妙惟肖是什么意思| 淡定从容是什么意思| 美国为什么有哥伦比亚| nc是什么| 1014是什么星座| 怀孕肚子胀是什么原因| 死缓是什么意思| aoc是什么意思| 1月20是什么星座| 梦见吃饭是什么意思| 手心发热吃什么药| 蓝色的小药丸是什么药| 绿松石是什么| 胆结石切除胆囊后有什么影响| 巨蟹和什么星座最配对| 葡萄糖升高说明什么| 养神经的药是什么药最好| edf是什么意思| nuxe是什么牌子护肤品| 肚脐眼是什么穴位| 大人有大量是什么意思| 狗狗生产需要准备什么| 欧米茄属于什么档次| 呃逆什么意思| 军校出来能干什么| 一览无余什么意思| 隐翅虫咬人后用什么药| 墨鱼是什么鱼| 白果有什么功效| 橄榄油的好处和坏处是什么| 角化棘皮瘤是什么病| 玻尿酸是什么东西| 七一什么节| 摄取是什么意思| 什么是虎牙| 男性手心热是什么原因| 什么是蝴蝶宝宝| 二尖瓣微量反流什么意思| 秋老虎是什么意思| 属马的和什么属相不合| 日午念什么| 子宫增大是什么原因| 落日余晖什么意思| 什么是比值| 聚餐吃什么| 仰天长叹的意思是什么| 肺气肿有什么症状| 什么是燕麦| 三伏贴是什么| 陈惠敏和陈慧琳什么关系| 凤梨和菠萝的区别是什么| 一年一片避孕药叫什么| 发霉是什么菌| 为什么会心肌缺血| 越位是什么意思| 仰卧起坐有什么好处| 鹿角菜是什么植物| 蝙蝠飞到家里是什么预兆| 癫痫挂什么科| 女人脱发是什么原因| 百香果的籽有什么功效| 其实不然是什么意思| 口腔溃疡是什么| 三只手是什么意思| 九牛一毛是什么意思| 心率慢是什么原因| 下下签是什么意思| 什么是血尿| 做馒头用什么面粉| 偶发性房性早搏是什么意思| 甜瓜不能和什么一起吃| 满天星的花语是什么| 勒索是什么意思| 什么耳朵| 空调什么牌子好| 三花五罗都是什么鱼| 情妇是什么意思| 梁下放床有什么禁忌| 唯我独尊是什么意思| 复方丹参片治什么病| vcr是什么| 嘌呤是什么| 人的舌头有什么作用| 巨蟹女和什么座最配对| 农历4月14日是什么星座| 神经质是什么意思| 婴儿吓着了有什么症状| 头晕想吐是什么原因| 广东有什么城市| 肝内钙化灶是什么意思| 厘清和理清的区别是什么| 总胆汁酸高是什么原因| 百度

????????????????????????????????????????д??????

(Redirected from Bit shift)
百度 但并不意味着除了这五个“灰犀牛”,就没有别的。

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.

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.

?
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.

?
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)
?
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, termed a LUT2 lookup table, a.k.a. a Boolean function order k=2 (2 inputs).

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

The ternary equivalent is a LUT3 boolean function of order k=3 (three inputs), resulting in a table of 256 operations, and in computing is termed a Bitwise ternary logic instruction.

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.

  • x & y = y & x
  • x & (y & z) = (x & y) & z
  • x & 0xFFFF = x[14]
  • x & 0 = 0
  • x & x = x
  • x | y = y | x
  • x | (y | z) = (x | y) | z
  • x | 0 = x
  • x | 0xFFFF = 0xFFFF
  • x | x = x
  • ~(~x) = x
  • 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-14.
  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-14.
  4. ^ "INT13-C. Use bitwise operators only on unsigned operands". CERT: Secure Coding Standards. Software Engineering Institute, Carnegie Mellon University. Retrieved 2025-08-14.
  5. ^ "Operator (C# Reference)". Microsoft. Retrieved 2025-08-14.
  6. ^ a b "Near constant time rotate that does not violate the standards?". Stack Exchange Network. Retrieved 2025-08-14.
  7. ^ "Poor optimization of portable rotate idiom". GNU GCC Project. Retrieved 2025-08-14.
  8. ^ "Circular rotate that does not violate C/C++ standard?". Intel Developer Forums. Retrieved 2025-08-14.
  9. ^ a b "Constant not propagated into inline assembly, results in "constraint 'I' expects an integer constant expression"". LLVM Project. Retrieved 2025-08-14.
  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-14. Retrieved 2025-08-14.
  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
股票杠杆是什么意思 什么不可当 手上脱皮什么原因 复诊是什么意思 真菌镜检阳性是什么意思
视力模糊是什么原因引起的 仓鼠不能吃什么 教师节属什么生肖 脸上长粉刺是什么原因 人流挂什么科
新的五行属性是什么 皮蛋吃了有什么好处和坏处 市公安局局长什么级别 为什么印度人叫阿三 血糖高的人能吃什么水果
身上发热是什么原因 血糖高可以喝什么粥 砖红色是什么颜色 天麻起什么作用 月经是什么
火龙果什么时候吃最好hcv8jop4ns3r.cn 胃气不足吃什么中成药hcv9jop1ns1r.cn 12月5日什么星座wzqsfys.com 理事是什么职位hcv9jop8ns2r.cn 百香果什么季节成熟hcv8jop0ns8r.cn
牙齿松动了有什么办法能固齿吗hcv7jop9ns0r.cn 宜昌有什么特产hcv7jop9ns3r.cn 老母鸡煲汤放什么食材补气补血hcv9jop5ns4r.cn 什么是体外射精hcv8jop6ns6r.cn 议员在中国相当于什么hcv8jop2ns0r.cn
洋姜有什么功效与作用aiwuzhiyu.com 减肥吃什么比较好wuhaiwuya.com 杞菊地黄丸治什么病hcv9jop7ns0r.cn 孩子不说话挂什么科hcv7jop4ns6r.cn 女人每天喝豆浆有什么好处hcv8jop4ns8r.cn
氧氟沙星和诺氟沙星有什么区别hcv8jop2ns1r.cn 无缘无故流鼻血是什么原因xinmaowt.com 气管小憩室是什么意思yanzhenzixun.com 左边小腹痛什么原因女hcv9jop2ns9r.cn 中药什么时候喝jiuxinfghf.com
百度