Implementation of Java Integer conversion (with source code), understanding of modulus and complement

Source: Internet
Author: User
Tags bitwise modulus

Implementation of the 1.toBinaryString method

1  Public StaticString tobinarystring (inti) {2 returnTOUNSIGNEDSTRING0 (i, 1);3 }4 Private StaticString TOUNSIGNEDSTRING0 (intValintshift) {5 //assert shift > 0 && shift <=5: "Illegal shift value";6 intMag = integer.size-Integer.numberofleadingzeros (val);7 intchars = Math.max ((Mag + (shift-1))/shift), 1);8 Char[] buf =New Char[chars];9 TenFormatunsignedint (Val, shift, BUF, 0, chars); One  A //Use special constructor which takes over "buf". - return NewString (BUF,true); -}


This is the number of bits used to represent the int value in the form of a binary complement.

Why do you need to use the 2-in-twos complement to make a simple mention?

In simple terms, the complement is to take the inverse plus 1 to facilitate the subtraction as a plus minus number for the addition operation.

In a computer system, the values are stored in the complement. The main reason: the use of complement, the symbol and other bits can be processed uniformly, and subtraction can also be processed by addition. In addition, two complementary
If the highest bit (sign bit) has a carry, the carry is discarded when the number of yards is added

int is 4 bytes, altogether 32 bits. So size is a constant of 32

Integer.numberofleadingzeros (Val);

Returns the number of 0 bits before the highest bit (leftmost) 1 bits in the binary complement representation of the specified int value

So mag is meant to be calculated, excluding the effect of irrelevant bits in the 2 binary of the value (int is 4 bytes 32 bits)

Chars what meaning does not see too understand us first and try a number

Example 9 (00000000 00000000 00000000 0000 1001)

Mag=4 chars=4 temporarily still do not understand the use of chars and shift, after the comparison of 8 binary 16 conversion code will find that the shift value of 1 3 4 is the key to the Post-op (& operation). (2^1=2 2^3=8 2^4=16))

Continuing reading down the code will reveal that Formatunsignedint is the core function for implementing conversions


1Formatunsignedint (Val, shift, BUF, 0, chars);2 Static intFormatunsignedint (intValintShiftChar[] buf,intOffsetintLen) {3 intCharpos =Len;4 intRadix = 1 <<shift;5 intMask = radix-1;6  Do {7Buf[offset +--charpos] = integer.digits[val &mask];8Val >>>=shift;9} while(val! = 0 && charpos > 0);Ten  One returnCharpos; A}

It is easy to get the chars corresponding to Len length by comparing the pass parameter.

What is the principle of this computational length?

Mag + (SHIFT-1))/Shift Mag is the length of the Val binary, the highest bit must be 1 we cite an example of octal to illustrate
Val in 2 binary is mag because 2^3=8 so the 3 bits in the binary are equivalent to one of the 8 binary (which is very similar to the IP dot decimal)
Shoft-1 is to handle 10 11-bit cases 10/3=3 and the actual situation is 10-bit Val which is 4-bit octal number
The end:
chars = Math.max ((Mag + (shift-1))/shift), 1);

Radit<<shift equals 2 of shift times. That is, Radit corresponds to a binary conversion.

Mask = Radix-1, which is the size of the module in which the conversion is made
It is important to note that this is not the same as the complementary modulus. This modulo is to derive the last couple of Val corresponding binary values
For example 8 (0000)%7 (0000 0111) =1 buf The last value is 1

What does the Do while (when value is not 0 and length >0) in the loop?

Buf[offset +--charpos] This is a bit similar to high-precision operations, where values are written from the end.

The conversion of the binary is achieved by buf the number of 10 decimal bits into the array by writing a bit after the bitwise operation.

and Val & Mask This operation is actually modulo 2 operation, modulo 8 operation, modulo 16 operation (after attaching to the module and complement understanding).

Integer.digits[] in the API did not find the introduction, online search also found only integerdigits[n] integerdigits[n,2] Introduction. Combined withthe introduction and operation of integerdigits[n,2] to know The role of integer.digits[]

For example 9 modulo 2 after 00000000 00000000 00000000 0000 0001 should be only the last one is taken and converted to char type into the BUF array

The last string (buf,true) completes the binary conversion output.

The understanding of mould is attached at the end of text

Module concept: A unit of measurement is called modulo or modulus. For example, in a system with a modulus of 16-10 actually has the same effect as +6, i.e.

10 and 6 complement each other (complement) in the system of modulo 12. The hardware structure of a computer has only adders, so most operations must eventually be converted to addition.

On the use of the concept of the original code, anti-code to improve to facilitate the operation, that is, the operation of the computer is the complement between the addition of operations. Personally think: The concept of the mold in the 2 binary is and bitwise reverse is a thing. The emergence of the concept of the mold is for people to direct the inverse code calculation.

We know that the complement is anti-code plus 1 for example:-9 (10001001)-(11110110)-(111101

11) is actually-9 of the Die 128 after the 2 +1, why is the Die 128, the computer counter is 8 bits, the first bit is the sign bit so 8 bit actually can remember the number is 256-2^7=128

A mod B =-(-a mod b)-9 modulo 128 is (11110110).

What's the relationship between the complement and the mold?

The appearance of the die is to facilitate the computer to the negative number directly with the symbolic bit of the addition operation. How does the module achieve this function?

In the dot-decimal conversion of the IP address of the computer network, it is often found

255=2^7+2^7-1;2^7=2^6+2^5 ....... 2^1+2^0+12^k=2^ (k-1) +2^ (k-2) +............+2^1+2^0+1// This can be demonstrated by an iterative approach to 2^k= (*2^) k-1 (=2^) k-1 (+2^ )// with k-1 to replace K namely K=k-12^ (k-1) =2^ (k-2) +2^ (k-2) and so on there will be 2^k=2^ (k-1) +2^ (k-2) +............+2^1+2^1 that is, the original proof.

What is the connection between this and the complement?

If there is a positive a=flag1*2^ (n-1) +flag2*2^ (n-2) + ... +flag (n-1) *2^1+flag (n) *2^0

Where flag takes 0 or 1

1 a=2^n-2^n+a
The original, inverse, and complement of positive numbers is not discussed.
Replace a 2^n and a on the right, with the following equation
2^n-a=2^ (n-1) +2^ (n-2) +............+2^1+2^0+1-(flag1*2^ (n-1) +flag2*2^ (n-2) + ... +flag (n-1) *2^1+flag (n) *2^0)
2^n-a= (1-FLAG1) *2^ (n-1) + (1-FLAG2) *2^ (n-2) +............+ (1-FLAGN) *2^0+1

(1-FLAG1) *2^ (n-1) + (1-FLAG2) *2^ (n-2) + ..... The right side of the line: Because flag is only 0.1 actual this is the bitwise inverse of the last +1
2^n-a:2 's n-th square is the modulo point already mentioned above

The final: modulo-Original code = Inverse Code

For the processing of the first sign bit and the interpretation of the symbolic bit as the numeric bit, the first blog level is limited gentlemen forgive

Implementation of Java Integer conversion (with source code), understanding of modulus and complement

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