JAVA Binary Basics

Source: Internet
Author: User
Tags bitwise

Main content

1. Decimal Binary Mutual Transfer

2. Binary bit operations

3.JDK built-in conversion

The binary in 4.JAVA

decimal binary Mutual transfer

57 111001

Binary bit operations:Advantages: In specific cases, the calculation is convenient and is widely supported. ① Bitwise AND & (two bits full bit 1, results only 1) 0 and 0=0;0 with 1=0;1 and 0=0; 1 and 1=1; Example: 51 and 500110011---------------special usage of 00000101=00000001=1 bit operations: * zeroing, Take the specified bit in a number ② bitwise OR | (As long as there is a bit 1, the result is 1) 0|0=0; 0|1=1; 1|0=1; 1|1=1:51|5 =00110011---------------00000101=00110111 = 55 or special usage of the operation: it is often used to 1;③ XOR operation ∧ for some locations of a data (two corresponding bits are "XOR", the result is 1, otherwise 0) 0∧0=0; 0∧1=1; 1∧0=1; 1∧1=0:51∧5=00110011---------------00000101=00110110=54 Special Usage: * Make certain bits Flip * with 0 xor or retain original value * Inverse operation (reverse the bitwise of a binary number, turn 0 to 1, 1 to 0) Using the method of exchanging values with 0 XOR, two variables: Two variable exchange values are: 1. With the third variable c=a; A=b; B=c;2. Using addition and subtraction c=a+b; B=c-b; A=c-b;3. Using bit XOR or operation to achieve (highest efficiency) a=a∧b; B =a∧b; A=a∧b; Left shift Operation << (the bits of an operand all left several bits, the left bits discarded, the right side 0) 2<<1= 4 Right Shift operation >> (will be an operand of the bits all right shift several bits, positive left 0, negative left to fill 1, Right discard) Unsigned right-shifted negative numbers represent the original code in the complement of their positive values: a positive number is converted to binary by absolute size called the original code. Anti-code complement: Anti-code plus 1 called the complementJDK built-in conversion:

binary in Java:Usually developed, the conversion and bit operation is not much, because Java processing is high-level in the cross-platform use more, file read and write, data communications and so on. Basic data type: INT data class: 1 bytes = 8bitbyte (8bit,-128~127) short (16bit) int (32bit) long (64bit) float: Single precision 32bit, double precision 64bitbolean:true 1bit,false 1bitchar:unicode character 16-bit corresponding wrapper class: Integer ... Other types that drop the base data type in Java are reference data types, and string is a class, so string is not a base type but a reference type.

Data type converted to byte: Int (8143) 8143 (00000000,00000000,00011111,11001111) =byte[]={-49,31,0,0} first byte (Low end) 8143>>0*8& 0xff= (11001111) =207 or signed-49 two bytes (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 first &0xff calculate the correct answer? First of all, the binary in Java uses the complement form, not the original code or the inverse code, these 3 concepts to be clear; second, byte is 8 bits, int is 32 bit, when Byte is cast to int, if not done & 0xFF, and byte corresponding value is negative, The upper 3 bytes are interpolated, so there is a possibility of errors in the complement error. For example, a byte type of-1, the second binary (complement) of 11111111 (that is, 0xff), converted to int, the value should also be-1, but after the complement, The resulting binary is 11111111111111111111111111111111 (that is, 0xFFFFFFFF), which is not-1, right? And the 0xFF default is int, so, a byte and 0xFF phase, will first convert that byte to int type operation, so that the result of the high 3 bytes will always be cleared 0, so the result is what we want ~ string into 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 means "gb2312,utf-8" encoding method
1  Public classConvert {2 3     /**4 * << left shift arithmetic << right shift operation <<< unsigned right shift5      */6 7     //int turn byte[]8      Public Static byte[] Int2bytes (intID) {9         byte[] arr =New byte[4];TenArr[0] = (byte) ((int) (ID >> 0 * 8) & 0xFF); OneARR[1] = (byte) ((int) (ID >> 1 * 8) & 0xFF); AARR[2] = (byte) ((int) (ID >> 2 * 8) & 0xFF); -ARR[3] = (byte) ((int) (ID >> 3 * 8) & 0xFF); -         returnarr; the     } -  -     //byte[] Turn int -      Public Static intBytes2int (byte[] arr) { +         intRs0 = (int) ((Arr[0] & 0xff) << 0 * 8); -         intRs1 = (int) ((Arr[1] & 0xff) << 1 * 8); +         intRS2 = (int) ((Arr[2] & 0xff) << 2 * 8); A         intRS3 = (int) ((Arr[3] & 0xff) << 3 * 8); at         returnRs0 + rs1 + rs2 +Rs3; -     } -  -     //long converted to byte[] -      Public Static byte[] Long4bytes (LongID) { -         byte[] arr =New byte[8]; in          for(inti = 0; i < arr.length; i++) { -Arr[i] = (byte) ((int) (ID >> i * 8) & 0xFF); to         } +         returnarr; -     } the  *     /** $ * byte[] conversion to long and conversion int, just long is 64bit, and int is 32bitPanax Notoginseng      */ -      Public Static voidMain (string[] args) { the         byte[] arr = Convert.int2bytes (8143); +System.out.println (arr[0] + "\ n" + arr[1] + "\ n" + arr[2] + "\ n" + arr[3]); A         intrs =Convert.bytes2int (arr); the System.out.println (RS); +  -         byte[] arr2 = Convert.long4bytes (20); $          for(byteb:arr2) { $ System.out.println (b); -         } -  the         //string and byte array: -String describle = "I am a string";Wuyi         byte[] Barr =describle.getbytes (); theString des =NewString (Barr); - System.out.println (DES); Wu     } -  About}
View Code

JAVA Binary Basics

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.