Some basic knowledge of Java binary

Time:2019-8-13

Explain

Everything has a specification, mention JAVA will mention two specifications, JAVA language specification, JVM specification. JAVA language specification mainly defines JAVA’s grammar, variables, types, grammar and so on. JVM specification mainly defines Class file type, run-time data, frame stack, virtual machine start-up, virtual machine instruction set and so on.

  • The JAVA language specification mainly defines what is the JAVA language.
  • The 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:

  • Plastic: byte, short, int, long
  • Floating point type: float, double
  • Boolean
  • Character type: char
  • Number of digits occupied by data types
data type Number of digits occupied
int 32bit
short 16bit
long 64bit
byte 8bit
char 16bit
float 32bit
double 64bit
boolean 1bit

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

Representation of integers

  • Source: The first bit is the symbol bit (0 is positive, 1 is negative).
  • Inverse Code: The symbol bits are fixed, and the original code is reversed.
  • Negative complement: symbol bit fixed, inverse code plus 1.
  • Positive complement: same as source code.

Note: Benefits of complements:

Complements can be used to represent 0 without any ambiguity.

Complements can participate in binary operations very well, and complements plus symbol bits can participate in operations, so it is much simpler.

Floating-Point Number Representation

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

Let’s illustrate with float:

IEEE745 single-precision floating-point format consists of 32 bits, including three fields: 23-bit decimal f, 8-bit offset index e, and 1-bit symbol s. These fields are consecutively stored in a 32-bit word and coded. Among them, 0:22 bits contain 23-bit decimal f; 23:30 bits contain 8-bit index e; and 31 bits contain symbol s.

A real number V can be expressed in the form of V=(-1)s*M*2E in the IEEE 754 standard, as follows:

  • Symbol s (sign) determines whether the real number is positive (s = 0) or negative (s = 1), and deals with the symbolic bits of the value 0.
  • Significant number M is a binary decimal. The value range of M is 1 < M < 2 or 0 < M < 1.
  • Exponent E is the power of 2. Its function is to weigh floating point numbers.
Symbolic bits Exponential position Decimal digits
1 place 8 place 23 place

For example, according to IEEE745, the single-precision floating-point values of 1100000100000000000000000000000000000 are calculated.

Solution:

1 10000010 00100000000000000000000
Symbolic bits index Because the exponent is not all zero, the decimal digit is added 1.
1 10000010 1.00100000000000000000000
-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 binary form of decimal floating point 0.1 is correct. You will find that 0.1 cannot be expressed as a finite number of binary bits, so the representation in memory is the result of rounding, that is, 0x3dcccd, decimal 0.100000001, resulting in an error of 0.000000000001.

The concept of Radix

We often use binary, octal, decimal and hexadecimal, decimal is the main form of expression.

Binary is 0 and 1; octal is 0-7; decimal is 0-9; hexadecimal is 0-9 + A-F (case is acceptable).

Bitwise Operators

Bitwise and (&)

The result is 1:


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

Usage:

  • Zero-setting: If you want a unit to zeroing, then make all its binaries zero, as long as you want to agree with a numerical value that everyone is zero, the result is zero.
  • Take a specified bit in a number: find a number corresponding to the bit X should take, the corresponding bit of the number is 1, the rest of the bit is zero, this number and X “and operation” can get 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 get it.

Bit or (|)

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


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

Usage: It is often used to locate 1 at some position of a data. Find a number corresponding to the bit X should be set to 1, the corresponding bit of the number is 1, and the rest is zero. This number is in phase with X or may cause some position 1 in X.

For example, we can get the low four position 1 of X = 1010 0000 by using X | 0000 1111 = 1010 1111.

XOR operation (^)
* If the two corresponding bits are different, the result will be 1, otherwise it will be 0:*


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

Usage:

  • To flip a specific bit: find a number corresponding to the number of X to be flipped, the corresponding bit of the number is 1, the rest of the bit is zero, this number is different from the corresponding bit of X or can be obtained; for example: X = 1010 1110, so that X low 4-bit flip, with X ^ 0000 1111 = 1010 0001 can be obtained.
  • Unlike 0, or reserve the original value such as: X ^ 0000 0000 = 1010 1110
  • Method of exchanging values of two variables: 1. By means of the third variable: C = A; A = B; B = C; 2. Using addition and subtraction to realize the exchange of two variables: A = A + B; B = A – B; A = A – B; 3. Using bitwise exclusive or operation to achieve: using a number exclusive or itself equal to 0 and exclusive or operation conforms to the exchange law, for example: A = A ^ B; B = A = A ^ B; A = A ^ B;

Inverse operation (~)

For a binary number, the inverse is to change 0 to 1, 1 to 0:~1=0, ~0=1.

Left shift operation (< <)

Move all binary bits of an operation object to the left by several bits (left binary discarded, right zeroing) 2 < 1 = 4:10> 1 = 100 = 4

If the discarded high position does not include 1 when shifting left, then each left shift is one bit, which is equivalent to multiplying the number by 2. – 14 (binary: 1111 0010)< < 2= (1100 1000) (high includes 1, not in accordance with the rules)

Right Shift (*)

All binary bits of a number are moved to the right by several bits, positive left complement 0, negative left complement 1, and right discard. Each operand is moved one bit to the right, which is equal to dividing the operand by two.

Left complement 0 or complement 1 depends on whether the number shifted is positive or negative.

Example: 4 > > > 2 = 1

Example: – 14 (1111 0010) > 2 = 4 (1111 1100)

Unsigned right shift operation (>)

Each bit moves to the right the specified number of digits. The prominent bits on the left after the right move are filled with zeros. Bits removed from the right are discarded

Each bit moves to the right the specified number of digits. The prominent bits on the left after the right move are filled with zeros. Bits removed from the right are discarded

For example: – 14 > 2

That is – 14 (1111 1111 1111 1111 1111 1111 1111 1111 10010)> 2

=(0011 1111 1111 1111 1111 1111 1111 1100)

= 1073741820

Explain:

  • 0x80000000 is a hexadecimal representation of numbers, converted into a binary representation of 10000000000000000000000000
  • Priority of operation, shift operation is higher than logical operation, >> higher than logical operation.&
  • Bit Logic and Operations 1-1 = 1, 0-1 = 0
  • Shift to the right without sign, remove part of the discarded, and add 0 to the left.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]