Bitwise [Reserved]


Bit computing

 

First of all

To understand bit arithmetic operations in binary mode, all other operations a few undergoing bit hex must first be converted into binary numbers further operation.
Bitwise includes bitwise AND (&), bitwise OR (|), bitwise exclusive or (^), negation (~), left (<<), right (>>) these types.
Wherein addition negated (-), other operators are two items, i.e., left and right sides of the operator has required a calculation amount.

Complement

Complement binary form to represent a negative number.
Approach is first transformed negative as positive then converted into binary form each bit of the binary number positive plus a negated.

For example, -5
to 5 obtains binary number: 000000000101
and then the individual bits becomes 0 becomes 0 1,1: 111111111010
Last plus 1: 111111111011
where 5 indicates that the binary 101 is all 0 omitted here to write only a few mean what ^ _ ^

Bitwise AND (&)

After two operands, and binary conversion operator, with (&) operation.
When the number is 1 in the corresponding bit, the bit takes a 1, otherwise as 0.

For example, 5 & -5
5: 0000 0101 0000
-5: 111111111011
Answer: 000000000001

Bitwise OR (|)

After two operands, and converted to binary, with or operator (|).
1 as long as the presence of the corresponding bit, this bit will take 1, if none is 1, 0.

Or 5 | -5
0000 0000 0101
1111 1111 1011
can be seen in every one of which has an
answer: 111111111111

Bitwise exclusive OR (^)

Of two operands, converted into a binary number, XOR (^) operation
if the same number at the corresponding position, this bit set to 0, if the bit takes a different change.

5 ^ -5
0000 0000 0101
1111 1111 1011
answer: 111111111110

While any number of XOR 0 is in itself, a different number if it is equal to 0 or yourself
the value of two numbers so that we can use to exchange XOR

For example exchange x, y values
X ^ = Y; X = X ^ Y
Y ^ = X; Y = Y ^ X ^ Y
X ^ = Y; X = (X ^ Y) ^ (Y ^ X ^ Y); // --------- final step x ^ = y when x = x ^ y; y = y ^ x ^ y

Left (<<)

The number in a binary number to the left by a number of bits,
such as x << y x is to the left of the binary bits y

Example: << 5 5
5: 0000 0101 0000
5 << 5: 000,010,100,000
in decimal is equal to 160

We can think about, in decimal, a number of once every 10 to take a left into.
So in binary, decimal with the same binary multiply once every 2 to enter a left,
then a number of left x is equivalent to a multiplication 2 x .

Right shift (>>)

The number to the right a plurality of binary bits in
the left same usage

<< Example 5 2
5: 0000 0101 0000
5 << 2: 000000000001
Decimal equal to 1

Here similar to the left, in addition to each of the 10-bit decimal integer one on the back
so the right is equivalent to 2 times except
while a right shift operation is rounded down to

The negated (-)

In fact, after saying that complement, negate it has been said, is the negation of the binary number at each bit of opposite number

5 : 0000 0000 0101
~5 : 1111 1111 1010

 


 

Disclaimer: This article is a blogger ( BIGBIGPPT ) original article, follow the  CC 4.0 by-sa  copyright agreement, reproduced, please attach the original source link and this statement.

This link: https://blog.csdn.net/BIGBIGPPT/article/details/88919783

Guess you like

Origin www.cnblogs.com/phemiku/p/11421813.html
Recommended