Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the bit operations and shift operations in Java

Shulou Source: shulou.com Published: 2022-06-02 05:32:30 10月25日 Update

This article introduces the relevant knowledge of "what are the bit operations and shift operations in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Bitwise operation "and" &

Rule: if the corresponding number of digits of the two corresponding binary forms is 1, the result is 1; otherwise, 0

4 & 5

four

0000 0100

five

0000 0101

Bitwise and operation

&

4 & 5 = 4

0000 0100

1 * 2 ^ 2 = 4

-4 & 5

-4

1111 1100

five

0000 0101

Bitwise and operation

&

Rule: the symbol bit of the operation result is 1, which is negative, which needs to be subtracted by 1, and then reversed (other operations should also be followed)

-4 & 5 = 4

0000 0100

1 * 2 ^ 2 =-4

1. Direct binary operation of positive number

two。 If you add one to the negative number to get the complement, the result symbol is 0, and you don't need to do anything to give the result directly.

Bitwise "OR" |

Rule: 1 is 1; otherwise, 0

4 | 5

four

0000 0100

five

0000 0101

Bitwise or operation

| |

| 4 | 5 = 5 |

0000 0101

1 * 2 ^ 0 + 1 * 2 ^ 2 = 5

-4 | 5

-4

1111 1100

five

0000 0101

Bitwise or operation

| |

Rule: the symbol bit of the operation result is 1, which is negative, which needs to be subtracted by 1, and then reversed (other operations should also be followed)

-4 | 5 =

1111 1101

Minus 1

1111 1100

Take reverse

-0000 0011

-4 | 5 =-3

1 * 2 ^ 0 + 1 * 2 ^ 1 =-3

XOR ^

Rule: the same is 0; the difference is 1

4 ^ 5

four

0000 0100

five

0000 0101

Bitwise XOR operation

^

4 ^ 5 = 1

0000 0001

1 * 2 ^ 0 = 1

-4 ^ 5

-4

1111 1100

five

0000 0101

Bitwise XOR operation

^

Rule: the symbol bit of the operation result is 1, which is negative, which needs to be subtracted by 1, and then reversed (other operations should also be followed)

-4 ^ 5 =

1111 1001

Minus 1

1111 1000

Take reverse

-0000 0111

-4 ^ 5 =-7

1 * 2 ^ 0 + 1 * 2 ^ 1 + 1 * 2 ^ 2 =-7

Shift operation to the left 2

-8

1111 1000

> > 2

1111 1110

Left shift operation

> >

Rule: the symbol bit of the operation result is 1, which is negative, which needs to be subtracted by 1, and then reversed (other operations should also be followed)

Minus 1

1111 1101

Take reverse

-0000 0010

-8 > > 2 =-2

1 * 2 ^ 1 =-2

Move unsigned right >

Rule: the positive number is the same as the right shift rule; the negative number moves to the right unsigned and adds 0 in the high position.

8 > > 2

eight

0000 1000

> > 2

0000 0010

Unsigned right shift operator

> > >

Result

8 > > 2 = 2

1 * 2 ^ 1 = 2

-8 > > 2

-8

1111 1111 1111 1000

> > 2

0011 1111 1111 1111 1110

Unsigned right shift operator

> > >

Rule: the symbol bit of the operation result is 1, which is negative, which needs to be subtracted by 1, and then reversed (other operations should also be followed)

Minus 1

-

Take reverse

-

-8 > > 2 = 1073741822

1073741822

This is the end of the introduction of "what are the bit operations and shift operations in Java?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Operations rules results symbols negative numbers operators positive numbers high bits binary content more knowledge vacated different practical the same learned and then two digits Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Microsoft macOS Huawei