Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the bit operations and shift operations in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 288

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report