Java Binary Related basics

Source: Internet
Author: User
Tags bitwise bitwise operators

Reprint please indicate original source, thank you!

Say in front

Before the JVM rookie advanced Master of the Road 10 (the basic knowledge of the introductory remarks) when the simple mention of binary related issues, recently in the source of the ROCKETMQ, found that involves binary content is quite many, JDK source is also a lot of related to the operation of binary, today this article is only a literacy article, The following will introduce the flexible use of the article.

Description

Everything has a specification, referring to Java will refer to 2 specifications, Java language Specification, JVM specification. The Java language specification primarily defines Java syntax, variables, types, grammars, and so on, and the JVM specification primarily defines class file types, runtime data, frame stacks, virtual machine launches, virtual machine instruction sets, and so on.

    • The Java language specification primarily defines what the Java language is.
    • The JVM specification mainly defines internal JVM implementations, binary class files, and JVM instruction sets.
Internal representation and storage of numbers in the specification

The eight basic data types of Java:
Shaping: Byte,short,int,long
Floating point type: float,double
Boolean Type: Boolean
Character type: Char

Data Type the number of occupied positions
Int 32bit
Short 16bit
Long 64bit
Byte 8bit
Char 16bit
Float 32bit
Double 64bit
Boolean 1bit

Note:1 bytes = 8 bits (1 byte = 8bit)

Representation of integers
    • Source: The first bit is the sign bit (0 indicates a positive number and 1 is a negative number).
    • Anti-code: the symbol bit fixed, the original code to take the reverse.
    • Negative complement: Sign bit fixed, anti-code plus 1.
    • Positive complement: Same as the source code.

      Note: The benefits of complement:
      • The use of complement can be without any ambiguity of the expression 0.
      • The complement can be very well involved in the binary operation, complement the symbolic bit to participate in the operation, so much easier.
Floating-point numbers indicate

In, we learned that both float and double are supported by IEEE 754

We use float to illustrate:

IEEE754 single-precision floating-point format consists of 32 bits, consisting of three constituent fields: 23-bit fractional f,8 bit-biased exponent e,1 bit sign S. These fields are kept in a 32-bit word and encoded. where 0:22 bits contain 23 bits of fractional F; 23:30 bits contain 8-bit exponent e; the 31st bit contains the symbol s.

A real number V can be expressed in the form of v= ( -1) sxmx2e in the IEEE 754 standard, as described below:

    • The symbol s (sign) determines whether the real number is positive (s=0) or negative (S=1), and the symbolic bit of the value 0 is handled specially.
    • A valid number M (significand) is a binary decimal, and the range of M is 1≤m<2 or 0≤m<1.
    • Exponent E (exponent) is a power of 2, and its function is to weighting floating-point numbers.
Decimal
sign Bit decimal pointdigits
1 guests 8 Guests 23 Guests

For example, the value of 11000001000100000000000000000000 single-precision floating point is calculated based on IEEE754.

Solving:

1 10000010 00100000000000000000000
Sign bit Index Mantissa because the exponent is not all 0, so the decimal place additional 1
1 10000010 1.00100000000000000000000
-1 2^ (130-127) (2^0 + 2^-3)

Conclusion:
-1 * (2^0 + 2^-3) * 2^ (130-127) =-9

Also, you can verify that the binary form of the decimal floating-point 0.1 is correct, and you will find that 0.1 cannot be represented as a finite bits, so the representation in memory is the result of rounding (rounding), i.e. 0X3DCCCCCD, decimal 0.100000001, The error 0.000000001 is thus generated.

Speaking of which, the JVM rookie Advanced Master of the Road 10 (basic knowledge of the introductory remarks) Some of the questions are actually answered, so the decimal type involving money must use BigDecimal, prohibit the use of float and double.

The concept of the binary system

We commonly use binary, octal, decimal, and hexadecimal, and decimal is the most important form of expression.

Binary is 0 and 1, octal is 0-7, decimal is 0-9, hexadecimal is 0-9+a-f (uppercase and lowercase).

Bitwise operators Bitwise-AND (&)

Two bits are all 1, the result is 1:

0&0=00&1=01&0=0;   1&1=1

Usage:

    • Clear 0: If you want a unit to clear zero, then make it all binary to 0, as long as with a everyone is zero value to want with, the result is zero.
    • Take a number middle finger positioning: Find a number, corresponding to the x to take the bit, the corresponding bit of the number is 1, the remaining bits are zero, this number and X for "and operations" can be obtained in the X of the finger positioning.

For example: Set x=1010 1110, take x low 4 bits, with x & 0000 1111 = 0000 1110 can be obtained.

Bitwise OR (|)

As long as there is a 1, the result is 1:

0|0=0;  0|1=11|0=11|1=1;

usage: often comes to a certain location of a data 1; Find a number, corresponding to the x to set 1 bits, the corresponding bit of the number is 1, the remaining bits are zero. This number is in X-phase or can make some position in X 1.

For example: Place the low four position of the x=1010 0000 1, with x | 0000 1111 = 1010 1111 can be obtained.

Xor operation (^)

Two the corresponding bit is "XOR" (the value is different), then the bit result is 1, otherwise 0:

0^0=0;0^1=1;1^0=1;1^1=0;

Usage:

    • Make a specific bit flip: Find a number, corresponding to the X to flip, the corresponding bit of the number is 1, the rest of the bits are zero, this number and x corresponding bit XOR can be obtained;
      For example: x=1010 1110, make x low 4 bit flip, with x ^ 0000 1111 = 1010 0001 can be obtained
    • Unlike 0 or, keep the original value
      Example: X ^ 0000 0000 = 1010 1110
    • Two ways to exchange values of variables:
      1, the use of the third variable to achieve: c=a; A=b; B=c;
      2, the use of addition and subtraction to achieve the exchange of two variables: a=a+b; B=a-b; A=a-b;
      3, using a bitwise XOR OR operation to achieve: the use of a number of different or itself equals 0 and XOR operation in line with the Exchange law
      For example: a = a ^ B; b = A ^ b; A = a ^ B;
Take inverse operation (~)

For a binary number bitwise reversed, will be 0 to change to 0: ~1=0; ~0=1;

Left shift operation (<<)
    • Shifts all bits of an operand to the left of a number of bits (left binary discard, 0 on the right)
      2<<1 = 4:10 <<1 =100=4
    • If the left-hand side does not include 1, then each left-shift is equal to the number multiplied by 2.
      -14 (binary: 1111 0010) << 2 = (1100 1000) (High level includes 1, non-conforming rules)
Right shift operation (>>)

All bits of a number are shifted to the right by a number of digits, positive left 0, negative left 1, and right discard. The operand is shifted one bit to the right, which is equal to the number divided by 2.

Left complement 0 or 1 depends on whether the number of shifts is positive or negative.
Example: 4 >> 2 = 1
Example: -14 (1111 0010) >> 2 = 4 (1111 1100)

Unsigned Right shift operation (>>>)

Shifts the individual bits to the right by the specified number of digits. The left prominent bit is filled with zero after the right shift. Move out the right bit is discarded
Shifts the individual bits to the right by the specified number of digits. The left prominent bit is filled with zero after the right shift. Move out the right bit is discarded
Example: -14>>>2
-14 (1111 1111 1111 1111 1111 1111 1111 0010) >>> 2
= (0011 1111 1111 1111 1111 1111 1111 1100)
= 1073741820

Binary representation of Java-printed integers
int1120429670;for (int032; i++) {    int0x80000000 >>> i) >>> (31 - i);    System.out.print(t);}

Description

  • The 0x80000000 is the hexadecimal representation of the number, which is converted to binary representation as 10000000000000000000000000000000
  • The priority of the operation, the shift operation is higher than the logical operation,>>> higher than &
  • Bit logic and Operation 1&1 = 1, 0&1 = 0
  • >>>Unsigned right shift, move out partial discard, left bit to fill 0;
Welcome to actively comment on your practical use of the understanding of the binary system of some good practice, look forward to your message!!!

If you have finished reading and feel the harvest, welcome to praise, attention, add the public number "ingenuity Zero".

Personal public number, welcome attention, see more Wonderful history!!!

Java Binary Related basics

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.