JAVA binary basics and java binary Basics

Source: Internet
Author: User

JAVA binary basics and java binary Basics
Main Content

1. Decimal Binary Conversion

2. bitwise operation of Binary

3. JDK built-in hexadecimal conversion

4. hexadecimal in JAVA

Decimal Binary Conversion

57 111001

 

 

Binary bit operations: advantages: it is easy to compute and widely supported in certain situations. ① Bitwise AND & (two full bits 1, the result is 1) 0 and 0 = 0; 0 and 1 = 0; 1 and 0 = 0; 1 and 1 = 1; example: 51 and 500110011-------------00000101 = 00000001 = special usage of the 1-bit operation: * zeroes, take the position in a Number ② By bit or | (as long as there is one bit, the result is 1) 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1; example: 51 | 5 = 00110011---------------00000101 = 00110111 = 55 or special operation usage: it is often used for some positions of a data 1; ③ XOR operation (two corresponding bits are "different", then the result is 1; otherwise, it is 0) 0 then 0 = 0; 0 running 1 = 1; 1 running 0 = 1; 1 running 1 = 0; example: 51 running 5 = 00110011---------------00000101 = 00110110 = 54 special usage: * enable special location flip * to be different from 0 or retain the original value * To take the inverse operation (bitwise inversion of a binary number, that is, changing 0 1, 1 to 0) the exchange value of two variables is: 1. the third variable C = A; A = B; B = C; 2. using addition and subtraction C = A + B; B = C-B; A = C-B; 3. bitwise exclusive or (most efficient) A = A ∧ B; B = A ∧ B; A = A ∧ B; left shift operation <(shifts all the binary bits of an operation object to several bits left, discards the binary bits on the left, and adds 0 to the right) 2 <1 = 4 right shift operation> (this operation shifts all the binary bits of an operation object to several right bits. The positive number is left 0, the negative number is left 1, and the right side is discarded) unsigned negative shifts to the right represent the original code in the form of a positive complement: a positive number is converted to a binary value based on the absolute value. Anti-Code complement: the anti-code plus 1 is called the built-in hexadecimal conversion of the complement JDK:

 

In JAVA: during normal development, there are not many hexadecimal conversions and bit operations, Because JAVA processes a large number of high-level cross-platform operations, such as file read/write and data communication. Basic Data Type: int data type: 1 byte = 8 bitbyte (8bit,-128 ~ 127) short (16bit) int (32bit) long (64bit) float: single precision 32bit, Double Precision 64 bitbolean: true 1bit, false 1 bitchar: unicode character 16-bit corresponding packaging class: integer ...... in JAVA, other types except the basic data type are reference data types. String is a class, so String is not a basic type but a reference type.

Data type conversion to byte: int (8143) 8143 (bytes, bytes 0000, 00011111,11001111) = byte [] = {-49,31,} first byte (low-end) 8143> 0*8 & 0xff = (11001111) = 207 or-49 second byte (low-end) 8143> 1*8 & 0xff = (00001111) = 31 third byte (low-end) 8143> 2*8 & 0xff = (00000000) = 0 fourth byte (low-end) 8143> 3*8 & 0xff = (00000000) = 0 byte to int, why do we have to calculate the correct answer first? First, the binary in JAVA uses the complement form instead of the original or reverse code. The three concepts need to be clarified. Second, byte occupies 8 bits and int occupies 32 bits, when a byte is forcibly converted to the int type, if the & 0xff operation is not performed and the value corresponding to the byte is negative, the three bytes in the upper position are supplemented, in this way, the makeup error may occur. For example, for byte-1, the binary value (complement Code) is 11111111 (0xff) and converted to int type. The value should also be-1, the resulting binary value is 11111111111111111111111111111111 (0 xffffffff). This is not-1, right? 0xff is of the int type by default. Therefore, a byte and 0xff phase will first convert that byte into an int type operation. In this way, the result will always be cleared by three bytes at the highest level, so the result is what we want ~ String to byte data: String s; byte [] bs = s. getBytes (); byte array converted to String: String s = new Stirng (bs); String s = new String (bs, encode); // encode indicates the encoding method "gb2312, UTF-8"
1 public class Convert {2 3/** 4 * <left shift operation <right shift operation <unsigned right shift 5 */6 7/int to Byte [] 8 public static byte [] int2Bytes (int id) {9 byte [] arr = new byte [4]; 10 arr [0] = (byte) (int) (id> 0*8) & 0xff ); 11 arr [1] = (byte) (int) (id> 1*8) & 0xff); 12 arr [2] = (byte) (int) (id> 2*8) & 0xff); 13 arr [3] = (byte) (int) (id> 3*8) & 0xff ); 14 return arr; 15} 16 17 // Byte [] to int18 public static int Bytes2int (byte [] arr) {19 int rs0 = (int) (arr [0] & 0xff) <0*8); 20 int rs1 = (int) (arr [1] & 0xff) <1*8 ); 21 int rs2 = (int) (arr [2] & 0xff) <2*8); 22 int rs3 = (int) (arr [3] & 0xff) <3*8); 23 return rs0 + rs1 + rs2 + rs3; 24} 25 26 // long is converted to byte [] 27 public static byte [] long4Bytes (long id) {28 byte [] arr = new byte [8]; 29 for (int I = 0; I <arr. length; I ++) {30 arr [I] = (byte) (int) (id> I * 8) & 0xff); 31} 32 return arr; 33} 34 35/** 36 * byte [] is converted to the long type, which is the same as the int type, but long is 64bit, the int value is 32bit37 */38 public static void main (String [] args) {39 byte [] arr = Convert. int2Bytes (8143); 40 System. out. println (arr [0] + "\ n" + arr [1] + "\ n" + arr [2] + "\ n" + arr [3]); 41 int rs = Convert. bytes2int (arr); 42 System. out. println (rs); 43 44 byte [] arr2 = Convert. long4Bytes (20); 45 for (byte B: arr2) {46 System. out. println (B); 47} 48 49 // String and byte array: 50 String describle = "I Am a String"; 51 byte [] barr = describle. getBytes (); 52 String des = new String (barr); 53 System. out. println (des); 54} 55 56}
View Code

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.