What is the maximum value of Int32??? (Java code with decimal hexadecimal conversions and contains positive negative numbers)

Source: Internet
Author: User
Tags pow

Positive binary binary is simple, and turning hex is simple.

What about the negative? The symbol "+", "-" that you gave to the computer is not recognized, and the computer knows only 0 and 1 so how to represent negative numbers in binary.

Let's briefly introduce how negative numbers go to binary, octal, 16 binary:

For example, 4.

Then the binary representation of +4 is calculated first:

1 0 0

But remember we need to make 0 in front, because a Int32 number in the computer is a memory space of 32, and the computer sees

0000 0000 0000 0000 0000 0000 0000 0100 is 4, this is the source

followed by the inverse code, the result is

1111 1111 1111 1111 1111 1111 1011

After the anti-code, +1 is 4 of the complement

1111 1111 1111 1111 1111 1111 1100

After getting 4 of the complement, in fact, this complement is-4 of the binary representation, then-4 of the octal is to convert every 3 bits into a numerical result is:

37777777774

Then 16 binary is every 4 bits converted into numbers

Fffffffc

Here's a question, according to the above rules,-12 binary means 1111 1111 1111 1111 1111 1111 1111 1111

Then 4294967295 numbers are converted to binary is also 1111 1111 1111 1111 1111 1111 1111 1111.

So what does 1111 1111 1111 1111 1111 1111 1111 1111 mean?

In fact:

A memory is 32 consecutive 1, you put it according to an int to interpret is-1, according to unsigned int is 4294967295

We can implement it in a C + + program:

int 4294967295 ;    cout<<aa;

The result is:

When you turn an int AA into a unsigned int AA unsigned represents an unsigned

So the result is 4294967295.

In C + +:

unsigned int:4294967295 (2^32-1)
Signed Int:2^31-1


Because, the computer needs to leave a top bit to determine whether you this number is positive or negative.

So int a number 32 digits under the maximum number is 2^31-1 you define more than this number then the computer will calculate you negative.

The following is a code for the decimal hexadecimal to be written by Java

 PackageCom.company;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONArray;ImportCom.alibaba.fastjson.JSONObject;ImportJava.util.HashMap;ImportJava.util.Map;Importjava.math.*;ImportJava.util.*; Public classMain {Static intBin[] =New int[1000]; Static CharHex[] =New Char[1000]; Static int_pos=0; Static intChange (Charx) {if(x>= ' 0 ' &&x<= ' 9 ')            returnX ' 0 '; Else if(x== ' A ')            return10; Else if(x== ' B ')            return11; Else if(x== ' C ')            return12; Else if(x== ' D ')            return13; Else if(x== ' E ')            return14; Else if(x== ' F ')            return15; Else            return16; }    //Hex Turn binary    Static voidHextobin () { for(inti=0;i)        {            intx=Change (Hex[i]); intJ=0; _pos+=4;  while(x>0) {bin[--_pos]=x%2; X/=2; J++; } _pos+=J; }        //minus 1, and then the inverse code .        if(hex.length==8&&hex[0]>= ' 8 ')        {            intXX =-1;  for(inti=_pos-1;i>=0;i--) {Bin[i]+=xx; if(Bin[i]==-1) {Bin[i]=1; XX=-1; }                Elsexx=0; }             for(inti=0;i<_pos;i++) {Bin[i]= (bin[i]==1?0:1); }        }    }    Static intBintodem () {intX=0;  for(inti=_pos-1;i>=0;i--) {x+ = (bin[i]* (int) Math.pow (2.0,_pos-1-i))); }        returnx; }     Public Static voidMain (string[] args) {//Write your code here//hashmap<string,hashmap<string,string>> m = new hashmap<> ();SYSTEM.OUT.PRINTLN ("********** input number, output 16 binary number"); Scanner input=NewScanner (system.in); X=Input.nextint ();    Demtobin (x);    Bintohex ();  for(inti=pos2-1;i>=0;i--) System.out.print (Hex[i]);    System.out.println (); //16 binary is negative, must be 8 bits, and the first bit is greater than or equal to 8SYSTEM.OUT.PRINTLN ("*********** input 16 decimal number, enter Number"); Hex=Input.next (). ToCharArray ();       Hextobin (); intx=Bintodem (); if(hex.length==8&&hex[0]>= ' 8 ') {System.out.println ("-"+x); }        ElseSystem.out.println (x); }    Static intBin[] =New int[10000]; Static CharHex[] =New Char[10000]; Static intPos2=0; Static intPOS =32; Static intPOS3 =0; Static intx; Static voidDemtobin (intx) {//convert first to binary        inty =Math.Abs (x); POS3=0;  while(y>0) {Bin[pos3++]=y%2; Y/=2; }        //if negative, you want to reverse the code        if(X < 0) {             for(inti = 0; I < POS; i++) {Bin[i]= (Bin[i] = = 1? 0:1); }            //plus 1            intXX = 1;  for(inti = 0; I < POS; i++) {Bin[i]+=xx; if(Bin[i] = = 2) {Bin[i]= 0; XX= 1; } Elsexx= 0; }            if(xx==1) Bin[pos++]=xx; }    }    Static CharChangeintx) {if(x>=0&&x<=9)        {            return(Char) (x+48); }        Else if(x==10)            returnA; Else if(x==11)            returnB; Else if(x==12)            returnC; Else if(x==13)            returnD; Else if(x==14)            returnE; Else if(x==15)           returnF; Else            returnG; }    //Binary turn 16 binary    Static voidBintohex () {intLen; if(x<0) Len=POS; ElseLen=Pos3; intj=0;intRes=0;  for(inti=0;i<len;i++) {res+=math.pow (2.0,J) *Bin[i]; J++; if(j==4) {Hex[pos2++]=Change (RES); Res=0; J=0; }            }            if(j!=0) {Hex[pos2++]=Change (RES); }    }}

What is the maximum value of Int32??? (Java code with decimal hexadecimal conversions and contains positive negative numbers)

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.