Bit operations in Java

Source: Internet
Author: User

Arithmetic  * */public class operator {public static void main of/* *  bit (String[] args)  {/* *  right Shift >> signed    positive 0  negative complement 1 *    >>> unsigned positive complement 0  negative complement 1 */int a = 10 ;//converted to binary, backward 2-bit front 0, The last two Bits erase System.out.println (A&GT;&GT;2);//move one equivalent to be in 2system.out.println (Integer.tobinarystring (a>>2));// Unsigned shift System.out.println (A&GT;&GT;&GT;2); System.out.println (integer.tobinarystring (a>>>2)) int b = -10 ;//front complement   complement 1   Erase/* *  reasoning process  * a. binary  28 01010 *   of binary  * 10 with 10 first to get back &NBSP;28 10101---"+1 * -10 binary  28 10110 * b. Move  1128 1 01  This is the final negative.  *  Right move two-bit  * c. Calculates the final result  *  negative negation +1 to get a corresponding positive number  * 0028 010 +1------"get corresponding positive number 00  28 011 *  Positive 3 corresponds to a negative number is -3 */system.out.println (B&GT;&GT;2);//Unsigned Right move specifically for negative numbers   Negative numbers are also 0system.out.pr in front.Intln (B&GT;&GT;&GT;2);/* *  left move operation << back 0 (regardless of positive or negative)  */system.out.println (a<<2);//left one, Multiply 2system.out.println (b<<2);}} &  (Bit and), | (bit or), ^ (XOR) Package com.pb.polytest;public class operator2 {public static void  main (String[] args)  {int c1 = 5;int d1 = 4;/* *  Convert two numbers to decimal and then one bit of the calculation  *  is 1 This bit is 1, otherwise 0 gets the binary converted to decimal  */system.out.println (C1&NBSP;&AMP;&NBSP;D1) ;/* *  convert two numbers to decimal and then one bit of calculation  *  is 0 This bit is 0, otherwise 1 gets the binary converted to decimal  */system.out.println (C1 &NBSP;|&NBSP;D1);/* *  convert two numbers to decimal then one bit of the calculation  *  the same as 0 different for the binary converted to decimal  */ System.out.println (C1&NBSP;^&NBSP;D1);/* *  if c =a ^ b ; *  so a ==  c ^ b ; * b == a ^ c ; */}}


This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1661079

Bit operations in Java

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.