Operators & | ^ ~ >> << explain

Byte is "byte," bit " 'bit; =. 8. 1' bit byte;

char is 2 bytes in java .java employed unicode, 2 bytes (16 bits) to represent a character.

Char 16 2 byte

byte 8 bits 1 byte

short 16 bit 2 of byte

int 32 bit 4 bytes

long 64 bits 8 bytes

float 32 bit 4-byte

double 64 bit, 8 byte

can count how many 0x7FFFFFFF
4bit each hexadecimal number, hexadecimal 8 is therefore 4 bytes, exactly one integer int
F 1111 binary code is
a binary code of 0111 7
as a result, the entire binary integer representation is 0x7FFFFFFF in addition to the first is 0, 1 are the rest
that is, this is the largest integer int (because the first one is a sign bit 0 indicates that he is a positive number)

bit operands operator is as a series of individual bits instead of a numeric value so that before, have to mention what the "bits":
values or characters are stored in memory as a sequence of 0 and 1, 0 and 1 are each referred to as a bits, for example, 10 decimal data is stored in the computer 2 is 00000010 (herein, one byte, for example, the specific number of bits, The decision to use a programming language), when we change the value of the bits in the memory, in this sense represents the value of it has changed, such as the mobile one, which is now before the storage unit 2 into a 0,000,010 0, this value represents the decimal 4, which is according to the principle of bit arithmetic operator.

bitwise operators six
& bitwise aND
| bitwise oR
Bitwise XOR ^
~ negated
>> right
<< left

1, & operator
& is a binary operator, if the corresponding bit combinations in the manner that the number of bits in a particular manner corresponding to the operation is 1, the result is 1 if either bit is a 0 result ie 0
1 & 1 is the result of 3
to look at how it runs:
a binary 1 is represented as 0,000,001
represented as binary 0 0 3 00011
results according to the rules & get is 00000001, decimal representation is 1
as long as no one is the result of the operation is 0 & 0, so you can use a variable & put unnecessary bit is set to 0, such as a binary variable is expressed as 01001001, I want to keep a low 4 to eliminate high with four & 0x0F on it (Note: 0x0F hexadecimal notation, corresponds to binary 00001111), this feature is widely used in the encoding.

2, | Operator
| with & distinction is that if any one bit corresponding to the operation is 1 the result is a
1 |. 3 Results of 3
3 ^ operators
^ operator with |, but a little different if the two operating positions are then 1, the results produce a '0
0 1 0 0 0 0 0 1
0 1 0 1 1 0 1 0
produce a' 0 0 0 1 1 0 1 1
4 ~ operator
~ is a bitwise negation 1 to 0, 0 to 1
5, the shift operators shift operators the bit shift value specified by the left or right
<< >> rightward and leftward movement, the excess bits are lost, and then fill the vacated bit 0
moves as 01000011 (decimal 67) to the left becomes two 67 << 2 to
 00001100 (decimal 12) Of course, if you use a java code written, because it is 32-bit, does not overflow, the result is 268
moving to the right are two 67 >> 2
000100 0 0 (decimal 16)

the following describes some specific applications
mentioned 2 becomes a forward movement (without considering the case of overflow and sign bit) can make use of this feature multiplication. 4
2 << 1 = 4
. 3 << 1 = 6
. 4 << 1 = 8
Similarly >> division can be done

with any fraction >> 0 it can be rounded
as 3.14159 = 0 3 >>;

^ arithmetic magical properties have served
as the following code
- --------------------
 

    int N1 = 3;
    int n2 = 4;
    N1 ^ = n2;
    n2 ^ = N1;
    N1 ^ = n2;

and finally we found, n1 = 4, n2 = 3, the value of both the swap

Guess you like

Origin www.cnblogs.com/riverone/p/11904667.html