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

How to understand the problem of moving left and right in Java

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

Share

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

How to understand the problem of moving left and right in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1 original code, inverse code, complement code

Original code: convert decimal to binary or original code

Inverse code: the inverse code of a positive number is the same as the original code, and the inverse code of a negative number (except that the symbol bit of the highest bit remains unchanged) is the opposite of the original code.

Complement: the complement of a positive number is the same as the original code, while the complement of a negative number (except that the symbol bit of the highest bit remains the same) is added to the original code.

Examples: 5 and-5

5:

Original code: 00000000 00000000 00000000 00000101

Inverse code: 00000000 00000000 00000000 00000101

Complement: 00000000 00000000 00000000 00000101

-5:

Original code: 1 00000000 00000000 00000000 00000101

Inverse code: 11111111 11111111 11111111 11111010

Complement: 11111111 11111111 11111111 11111011

2 displacement operation

We need to know how many bits to move a number in the computer, for example, 8 >: unsigned right shift, high zero filling.

Why is there no unsigned left shift? Because there is only zero filling in the case of moving to the left.

Examples: 5 and-5

fifty-one

Original code: 1 00000000 00000000 00000000 00000101

Complement: 11111111 11111111 11111111 11111011

Move 1 bit to the right: 11111111 11111111 11111111 11111101

Take its complement: 1 0000000 00000000 00000000 00000011

That is:-3

5 > 1: same as 5 > > 1, they are all high position complement 0.

Original code: 00000000 00000000 00000000 00000101

Move 1 bit to the right: 00000000 00000000 00000000 00000010

That is: 2

-5 > 1

Original code: 1 00000000 00000000 00000000 00000101

Complement: 11111111 11111111 11111111 11111011

Move 1 bit to the right: 0 1111111 11111111 11111111 11111101

That is, 2147483645

Screenshot of code running

Summary:

Displacement operation is very simple, the use of complement movement, fill the vacancy on the line, except > >: fill is consistent with the highest position; other movement which lack which fill zero.

In java, no matter which type an in the screenshot is defined as byte,short,int, it defaults to using the int type (4 bytes 32 bits) and you use long (8 bytes 64 bits).

3 supplementary operator

~ (bitwise non):

Bit by bit inversion

| (bitwise or):

One truth is true.

| 1 | 0room1 |

0 | 0: 0

1 | 1: 1

0 | 1: 1

& (bitwise and):

The two truths are true

| 1 | 00.00 |

0 | 0: 0

1-1-1

0-1-0

^ (bitwise XOR):

Difference is true

1 ^ 1 = 0

1 ^ 0 = 1

0 ^ 1 = 1

0 ^ 0 = 0.

This is the answer to the question about how to understand the left and right shift in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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: 0

*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