Java Basics: Decimal conversion Other binary

Source: Internet
Author: User
Tags bitwise

In peacetime use, the conversion of the binary as long as the use of integer this wrapper class method can be completed.

But in fact, we can use their own methods to achieve, which helps beginners understanding of the computer's underlying operations. The following example uses a table-based method to complete the conversion of a binary.

Before you begin, it is necessary to understand the concepts of the characteristics of each system and the bit.

As we all know, in the computer, memory is stored in binary data, that is, by 0, one to represent the content of the data.

bit is the smallest unit in the computer that represents every 0 or 1,bit of binary data.

It is important to note that the basic computer storage unit is byte (byte), which is represented by 8 bits, which is 1 byte = 8 bit.

Java can support octal, decimal, and hexadecimal representations, respectively:

Octal: Front 0, as in octal notation 5 to write 05.

Decimal: No front-facing, such as 5 is the decimal in the 5.

Hex: Front 0x or 0X, as in hexadecimal notation 5 to be written as 0x5.

If you generate data of type int with a value of 60, it consumes 32 bits of space in memory. As shown below:

0000-0000 0000-0000 0000-0000 0011-1100

Hexadecimal values are represented by the 16 numbers 0~9 and A~f , which are rounded when 16 is satisfied. 60 is represented in hexadecimal in Java as: 0x3C, which behaves in memory as:

The octal value is represented by the 8 number of 0~7, and is rounded when 8 is satisfied. 60 in Java, denoted by octal: 074, in memory as:

From the above rules can be seen, the binary in every 4 bits (2 of the Power of 4 =16), which represents 1 bits in hexadecimal, and to get the value on the 16 binary bits, you can use the bitwise operation & 15 (binary number 1111).

In the same vein, every 3 bits in the binary (2 of the 3 power =8) represent 1 bits in the octal, and to derive the values on the 8 digits, you can use bit arithmetic & 7 (binary number 111) to derive.

Based on the above analysis, you can design the code:

1 classTransform2 {3      Public Static voidMain (string[] args)4     {5Tohex (60);6Toba (60);7ToBin (60);8     }9 Ten //Decimal Turn binary One      Public Static voidToBin (intnum) A     { -Trans (num,1,1); -     } the  - //Decimal Turn octal -      Public Static voidToba (intnum) -     { +Trans (num,7,3); -     } +  A //Decimal Turn hex at      Public Static voidTohex (intnum) -     { -Trans (num,15,4); -     } -  - //specific conversion process.  in //The first parameter in the method is the number that needs to be converted, the second argument is the number on the bitwise Operation &, and the third parameter is the bits to move.  -      Public Static voidTrans (intNumintBaseintoffset) to     { + //defines an array, which corresponds to a value, and can be viewed as a query table.  -         Char[] chs = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '}; the         Char[] arr =New Char[32]; *         intpos =arr.length; $ Panax Notoginseng         if(num==0) -         { the //if the number to convert is 0, no conversion is required.  +SYSTEM.OUT.PRINTLN (0); A         } the  +          while(num!=0) -         { $ //the value of each bit in the specified binary is obtained and stored in an array.  $             inttemp = num &Base; -Arr[--pos] =Chs[temp]; - //The bits of the original value moves the specified number of digits to the right after each one is given a specified binary value, and 0 is prepended.  thenum = num >>> offset;//Shift-Fill 0.  -         }Wuyi          the          for(intx=pos;x<arr.length;x++) -         { Wu             if(x== (arr.length-1)) { - System.out.println (arr[x]);  About}Else { $ System.out.print (arr[x]);  -             } -         } -     } A}

Java Basics: Decimal conversion Other binary

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.