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 basic knowledge of java binary system

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the basic knowledge related to java binary system", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what are the basic knowledge related to java binary"!

description

Anything has specifications, Java will mention two specifications, Java language specifications, JVM specifications. JAVA language specification mainly defines JAVA syntax, variables, types, grammars, etc. JVM specification mainly defines Class file types, runtime data, frame stack, virtual machine startup, virtual machine instruction set, etc.

The Java language specification defines what the Java language is. JVM specification mainly defines JVM internal implementation, binary class file and JVM instruction set.

Internal representation and storage of numbers in specifications

Java has eight basic data types:

integer: byte,short,int,long float: float,double boolean character: char data type placeholder

int 32bit short 16bit long 64bit byte 8bit char 16bit float 32bit double 64bit boolean 1bit

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

integer representation

Source code: The first bit is the sign bit (0 means positive, 1 means negative). Reverse code: symbol bit does not move, the original code is reversed. Negative complement: the sign bit does not move, complement plus 1. Positive complement: Same as source code.

Note: Benefits of complement:

0 can be represented without any ambiguity using complement.

Complements can be very good to participate in the operation of two's, complement add sign bit to participate in the operation, so much simpler.

floating point number representation

In the figure above, we learned that Float and Double both support IEEE 754.

We use float to describe:

The IEEE745 single-precision floating-point format consists of 32 bits and consists of three constituent fields: a 23-bit decimal f, an 8-bit offset exponent e, and a 1-bit symbol s. These fields are stored consecutively in a 32-bit word and encoded. Where 0:22 contains the 23-digit decimal f; 23:30 contains the 8-digit exponent e; and the 31st contains the symbol s.

A real number V can be expressed in IEEE 754 as V=(-1)s×M× 2E, as follows:

The sign s(sign) determines whether a real number is positive (s=0) or negative (s=1), with special treatment of the sign bit for the value 0. The significant digit M (significance and) is a binary decimal, M values range from 1≤M<2 or 0≤M<1. The exponent E (exponent) is a power of 2, and its function is to weight floating point numbers.

1 bit 8 bits 23 bits

For example, according to IEEE745, calculate the single-precision floating-point value of 11000001000000000000.

Solution:

Sign digit exponent mantissa Since exponent is not all 0, decimal place is added 1 1 10000010 1.001000000000000-1 2^(130-127) (2^0 + 2^-3)

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

Similarly, you can also verify that the decimal floating point number 0.1 is correct in binary form, you will find that 0.1 cannot be represented as a finite number of binary bits, so the representation in memory is the result of rounding, i.e. 0x3dcccccd, decimal is 0.10000001, error 0.0000001.

The concept of base

We commonly use binary, octal, decimal and hexadecimal, 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 (both upper and lower case).

bitwise operators

Bitwise and (&)

If both digits are 1's, the result is 1:

0&0=0;0&1=0;1&0=0;1&1=1;

Number of data types occupied

Sign place exponent place decimal place 1100000100000000000000000

Usage:

Clear: If you want a unit to be cleared, then make it all binary 0, as long as you want to and a value with all bits being zero, the result is zero. Take a specified bit in a number: Find a number corresponding to the bit to be taken by X, the corresponding bit of the number is 1, the remaining bits are zeros, and this number and X can be "AND" to obtain the specified bit in X.

For example: Let X=1010 1110, take the lower 4 bits of X, and use X & 0000 1111 = 0000 1110 to obtain.

by location or|)

As long as one is 1, the result is 1:

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

Usage: often used to set some positions of a data to 1; find a number corresponding to X to set 1 bit, the corresponding bit of the number is 1, and the rest of the bits are zero. This number is ORed with X to make certain positions in X 1.

For example: X=1010 0000 low four position 1, with X| 0000 1111 =1010 1111 can be obtained.

Exclusive OR operation (^)* If the two corresponding bits are exclusive (different values), the result of this bit is 1, otherwise 0: *

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

Usage:

To flip a specific bit: Find a number corresponding to each bit of X to be flipped, the corresponding bit of the number is 1, and the remaining bits are zeros. This number and X corresponding bits are exclusive OR; for example: X=1010 1110, flip the lower 4 bits of X, and use X ^ 0000 1111 = 1010 0001 to obtain a different OR from 0. Keep the original value. For example: X ^ 0000 000 = 1010 1110 1. Use the third variable to realize: C=A; A=B; B=C; 2. Use addition and subtraction to realize the exchange of two variables: A=A+B; B=A-B;A=A-B; 3. Use bit exclusive OR operation to realize: Use a number exclusive OR itself equal to 0 and exclusive OR operation conforms to the commutative law. For example: A = A ^B; B = A ^ B; A = A ^ B;

negate operation (~)

For a binary number, reverse the bit, that is, 0 to 1, 1 to 0:~1=0; ~0=1;

Left shift operation (>>)

Shifts each digit to the right by the specified number of digits. The bits highlighted to the left after the right shift are filled with zeros. Bits shifted out to the right are discarded

Shifts each digit to the right by the specified number of digits. The bits highlighted to the left after the right shift are filled with zeros. Bits shifted out to the right are discarded

For example: -14>>>2

1111 1111 1111 1111 1111 1111 1111 0010>> 2

=(0011 1111 1111 1111 1111 1111 1111 1100)

= 1073741820

Description:

0x8000000 is the hexadecimal representation of the number, converted to binary representation, the priority of the operation, shift operation is higher than the logical operation,>>> higher than the & bit logical AND operation 1 &1 = 1, 0 &1 = 0 >>> unsigned right shift, shift out partial discard, left bit complement 0;

At this point, I believe that everyone has a deeper understanding of "what are the basic knowledge related to java binary system", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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