Java Basics-A text to understand bit arithmetic

Source: Internet
Author: User
Tags arithmetic bitwise

In everyday Java development, bit operations use little, more arithmetic operations (+ 、-、 *,/,%), relational operations (<, >, <=, >=, = =,! =), and logical operations (&&, | |,!), Therefore, relative to the bit operation is not so familiar with, this article will use Java bit operation to detail the next operation and its application.

1. bit Operation Origin

Bit operations originate from the low-level operation of the C language, and Java is designed to be embedded in the TV set-top box, so this low-level operation is preserved. The so-called low-level operation is because the operand of the bitwise operation is bits, but this kind of low-level operation is very simple, direct, and friendly and efficient for the computer. On simple low-cost processors, bit operations are usually much faster than division, several times faster than multiplication, and sometimes much faster than addition. Although modern processors typically perform addition and multiplication at the same speed as bit operations due to lengthy instruction pipelining and other architectural design choices, bit operations often use less power due to reduced resource usage, so clever use of bit operations in some Java underlying algorithms can reduce the overhead of running a lot.

2. bit Operation detailed

The subdivision of Java bit operations can be divided into bitwise operations and shift operations, as shown in the table below.

Refinement

Symbol

Describe

Arithmetic rules

Bitwise operations

&

And

Two bits are 1, so the result is 1.

|

Or

One is 1, so the result is 1.

~

Non -

~0 = 1,~1 = 0

^

XOR or

Two bits are different, the result is 1

Shift Operations

<<

Move left

All bits left n bits, high drop, low 0

>>

Move right

Each bits all right shift n bit, if the value is positive, then inserts 0 at the high, if the value is negative, then inserts 1 in the high position

>>>

Unsigned Right Shift

Each bits all right shift n bits, both positive and negative, are inserted in the high 0

Before the bit operation in detail, the first to popularize the digital representation of the computer. For the computer, all things are 0, 1, all the numbers will eventually be converted to 0, 1 of the expression, there are 3 forms of embodiment, namely: the original code, anti-code and complement .

Original code: The original code notation in front of the number of a sign, that is, the highest bit is the sign bit, the positive digits of the bit is 0, the negative digits of the bit is 1. For example, the decimal 5 if 8 bits means that 00000101,-5 is 10000101.

Anti-code: the inverse of a positive number is its own, negative anti-code in its original code on the basis of the symbol bit unchanged, the remaining bits are reversed. 5 of the inverse code is 00000101, and 5 is 11111010.

Complement: Positive complement is its own, negative complement in its original code based on the symbol bit unchanged, the rest of you take the counter, the last +1. That is, on the basis of the anti-code +1. 5 of the inverse code is 00000101, and 5 is 11111011.

After understanding these concepts, we will now remember a conclusion, that is, in the computer system, the numbers are used in the complement to express, compute and storage, the specific reasons can see the discussion of this article, there is no more discussion here, because it is not the focus of this article.

2.1 and Arithmetic (&)

Rule: After converting to binary, two bits is 1, then the result is 1, otherwise the result is 0.

Example:

Decimal

Binary (positive source code, inverse code, complement)

10

00000000000000000000000000001010

&12

&00000000000000000000000000001100

=

=

8

00000000000000000000000000001000

Decimal

Binary (original code)

-6

10000000000000000000000000000110

&-2

&10000000000000000000000000000010

Decimal

Binary (anti-code)

-6

11111111111111111111111111111001

&-2

&11111111111111111111111111111101

Decimal

Binary (complement)

-6

11111111111111111111111111111010

&-2

&11111111111111111111111111111110

=

=

-6

11111111111111111111111111111010

The final result of the calculation 11111111111111111111111111111010 or the complement of the form, to see its decimal, but also need to first turn into binary source code.

First turn the anti-code: 11111111111111111111111111111010-1=11111111111111111111111111111001, have anti-code 11111111111111111111111111111001.

Re-transfer the original code: on the basis of the anti-code to the original code, the sign bit unchanged, the other members to take the reverse, get 10000000000000000000000000000110. The first digit 1 represents a negative number, followed by 0110 turns to decimal is 6, to 6.

2.2 or Arithmetic (|)

Rule: After converting to binary, there is a one of 1, then the result is 1, otherwise the result is 0.

Example:

Decimal

Binary (positive source code, inverse code, complement)

10

00000000000000000000000000001010

|12

|00000000000000000000000000001100

=

=

14

00000000000000000000000000001110

Decimal

Binary (original code)

-6

10000000000000000000000000000110

|-2

|10000000000000000000000000000010

Decimal

Binary (anti-code)

-6

11111111111111111111111111111001

|-2

|11111111111111111111111111111101

Decimal

Binary (complement)

-6

11111111111111111111111111111010

|-2

|11111111111111111111111111111110

=

=

-2

11111111111111111111111111111110

2.3 Non-arithmetic (~)

Rule: After converting to binary, ~0 = 1,~1 = 0.

Example:

Decimal

Binary (positive source code, inverse code, complement)

The

~00000000000000000000000000000111

=

=

-8

11111111111111111111111111111000 (the complement needs to be converted to the original code)

11111111111111111111111111111000-1 have anti-code, you can think of 1000 as 0112, the anti-code 11111111111111111111111111110111. According to the inverse code of the original code 10000000000000000000000000001000.

Decimal

Binary (original code)

~ (-6)

~10000000000000000000000000000110

Decimal

Binary (anti-code)

~ (-6)

~11111111111111111111111111111001

Decimal

Binary (complement)

~ (-6)

~11111111111111111111111111111010

=

=

5

00000000000000000000000000000101 (positive source code, inverse code, the same complement)

2.4 xor operation (^)

Rule: After converting to binary, the two bits are different, the result is 1, otherwise 0.

Example:

Decimal

Binary (positive source code, inverse code, complement)

15^2

00000000000000000000000000001111

^00000000000000000000000000000010

=

=

13

00000000000000000000000000001101

2.5 left Shift operation (<<)

Rule: After converting to binary, each bits all left n bits, high drop, low 0.

Example:

Decimal

Binary (positive source code, inverse code, complement)

2<<2

00000000000000000000000000000010

=

000000000000000000000000000010xx

8

00000000000000000000000000001000

Decimal

Binary (first complement and complement operation shift)

-2<<2

10000000000000000000000000000010 (original code)

11111111111111111111111111111101 (anti-code)

11111111111111111111111111111110 (complement)

111111111111111111111111111110xx

11111111111111111111111111111000 (complement)

11111111111111111111111111110111 (anti-code)

-8

10000000000000000000000000001000 (original code)

2.6 Right Shift operation (>>)

Rule: After turning to binary, each bits all right shifts n bits, if the value is positive, then inserts 0 at the high position, if the value is negative, then inserts 1 in the high position.

Example:

Decimal

Binary (positive source code, inverse code, complement)

2>>2

00000000000000000000000000000010

=

xx000000000000000000000000000000

0

00000000000000000000000000000000

Decimal

Binary (first complement and complement operation shift)

-6>>2

10000000000000000000000000000110 (original code)

11111111111111111111111111111001 (anti-code)

11111111111111111111111111111010 (complement)

111111111111111111111111111110

11111111111111111111111111111110 (complement)

11111111111111111111111111111101 (anti-code)

-2

10000000000000000000000000000010 (original code)

2.7 Unsigned Right Shift operation (>>>)

Rule: After turning to binary, each bits all right shift n bits, both positive and negative, are inserted in the high 0.

Example:

Decimal

Binary (first complement and complement operation shift)

-1>>>1

10000000000000000000000000000001 (original code)

11111111111111111111111111111110 (anti-code)

11111111111111111111111111111111 (complement)

011111111111111111111111111111111

01111111111111111111111111111111 (complement)

01111111111111111111111111111110 (anti-code)

Overflow, can only represent a maximum value of 2147483647 to int

10000000000000000000000000000001 (original code)

3. Application

3.1 No additional variables for two-digit interchange

See Resources for Bitoperationtest, method reverse the substitution of two variable values through three XOR operations.

The proof is simple, we just need to understand that the XOR operation satisfies the following law (actually not only the following rules):

0^a = A,a^a = 0;

a ^ B = b ^ A;

A ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ C;

A ^ b ^ a = b;

Suppose A, B, two variables, the following steps to complete the value exchange: A=a^b,b=b^a,a=a^b.

The proof is as follows:

Because a ^ b = b ^ A, and a=a^b,b=b^a. Therefore b=b^a= b^ (a^b) =a.

Continue a=a^b,a= (a^b) ^ b^ (a^b), so a=b. Completes the value Exchange.

3.2 Absolute value without judgment statement implementation

The formula is as follows: (a^ (A>>31))-(A>>31)

First of all, the idea of using bit operation to get absolute value: If A is positive, then it is invariant, need to use XOR or 0 to maintain the characteristics; If a is negative, then its complement for the original code flip each bit after + 1, first seek its original code, complement-1 and then flip each bit, at this time need to use XOR or 1 has

Any positive number left after 31 only sign bit 0, the final result is 0, any negative number shifted right 31 is only left sign bit 1, overflow 31 bit truncation, vacated 31 bit complement sign bit 1, the final result is-1. Move right 31 to get the sign bit for any integer.

Then combine the above steps to get the formula. A>>31 obtains a symbol, if a is positive, a>>31 equals 0,a^0=a, unchanged; If a is negative, a>>31 equals-1, a^-1 flips each bit.

3.3 Determining the parity of a number

The pseudo-code is as follows:

N&1 = = 1? " Odd ":" Even "

The odd minimum bit is definitely 1, and 1 of the binary lowest bit is 1, the other bits are 0, so all odd and 1 and the result of the operation is definitely 1.

Resources:

Github.com/lingjiango/concurrentprogrampractice

En.wikipedia.org/wiki/bitwise_operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.