Explanation of Java bit operations

Source: Internet
Author: User

The following is a small summary of the Java bit operator, as shown below: </P> <p> A negative binary number is expressed in the form of a positive complement <br/>: the anti-code + 1 equals the completion Code <br/> For example: system. out. println (integer. tobinarystring (-5); <br/> Print: 1111 1111 1111 1111 1111 1111 1111 1011 no need to think about it is certainly a 32-bit <br/> the process is like this: <br/> a negative number, such as-5, in Java, the binary value indicates <br/> 1000 0000 0000 0000 0000 0000 0000 0101 <br/> its positive value is: <br/> 0000 0000 0000 0000 0000 0000 0000 0101 <br/> the backend code is: <br/> 1111 1111 1111 1111 1111 1111 1111 <br/> plus 1: <br/> 1111 1111 1111 1111 1111 1111 1111 1011 <br/> the printed content is obtained. </P> <p> JAVA ~ Returns the inverse of each binary bit, that is, 1 is changed to 0, 0 is changed to 1 </P> <p> test negative number: <br/> int A =-5; // 101; <br/> system. out. println (~ A); <br/> Print: 4 <br/> the process is as follows, first, this negative number is displayed. <br/> 1111 1111 1111 1111 1111 1111 1111 1011 (as mentioned above) <br/> obtain <br/> 0000 0000 0000 0000 0000 0000 0000 0100 <br/> convert to a 10-digit system to get 4 </P> <p> test positive number: <br/> int A = 5; // 101; <br/> system. out. println (~ A); <br/> Print:-6 <br/> indicates the positive number first: <br/> 0000 0000 0000 0000 0000 0000 0000 0101 <br/> you can obtain the following result: <br/> 1111 1111 1111 1111 1111 1111 1111 <br/> This represents-6. Why is it the top? </P> <p> JAVA & operator, perform and operate the corresponding binary bits. Both values are 1 to 1, and the values are 0. <br/> test: <br/> system. out. println (5 & 6); <br/> Print: 4 <br/> process: <br/> 5 indicates: 0000 0000 0000 0000 0000 0000 0000 0101 <br/> 6: 0000 0000 0000 0000 0000 0000 0000 <br/> proceed &: 0000 0000 0000 0000 0000 0000 0000 0100 <br/> expected result: 4 </P> <p> JAVA | Operator. The values of the two operators are 0 and the value of the two operators is 0, all other cases are 1 <br/> test: <br/> system. out. println (5 | 6); <br/> Print: 7 <br/> process: <br/> 5 indicates: 0000 0000 0000 0000 0000 0000 0000 0101 <br/> 6: 0000 0000 0000 0000 0000 0000 0000 <br/> |: 0000 0000 0000 0000 0000 0000 0000 0111 <br/> expected result: 7 </P> <p> when the Java ^ operator has the same binary value, this digit is 0 or 1 <br/> test: <br/> system. out. println (5 ^ 6); <br/> Print: 3 <br/> process: <br/> 5 indicates: 0000 0000 0000 0000 0000 0000 0000 0101 <br/> 6 indicates: 0000 0000 0000 0000 0000 0000 0000 <br/> ^: 0000 0000 0000 0000 0000 0000 0000 0011 <br/> expected result: 3 </P> <p> JAVA <operator, left-side movement, left-side filling, 0 <br/> test: <br/> system. out. println (5 <1); <br/> Print: 10 <br/> 5 indicates: 0000 0000 0000 0000 0000 0000 0000 0101 <br/> <1 operation: 0000 0000 0000 0000 0000 0000 0000 1010 </P> <p> JAVA> operator, left move, right fill 0 <br/> test: <br/> system. out. println (5 >>> 1); <br/> Print: 2 <br/> 5 indicates: 0000 0000 0000 0000 0000 0000 0000 <br/> 1 operation: 0000 0000 0000 0000 0000 0000 0000 0010 </P> <p> the value of one left shift is multiplied by 2. The value of one right shift is equivalent to 2. </P> <p> This article comes from: it Knowledge Network (http://www.itwis.com) detailed source reference: http://www.itwis.com/html/java/j2se/20090223/3407.html

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.