Java Base bit operations

Source: Internet
Author: User

http://www.verejava.com/?id=16992602784016

public class operation5{public static void Main (string[] args) {//bitwise operation/* Includes: with &, or |, take the inverse ~, xor ^ with symbol left shift <<, with the symbol right shift >>, unsigned right shift >>> 1. & is bits if all 1 is 1, otherwise 0 2. | Is bits if all is 0 is 0, otherwise 1 3.                ~ is bits if it is 0 to 1, otherwise if it is 1 take the reverse into 0; 4. ^ is the operation on both sides of the bits on both sides equal to 0, otherwise 1 5.                << is to move the bits bit to the left n bits, followed by n 0, equivalent to 2;                6. >> is to move the bits to the right by the N-bit, the first fill n the sign bit, to when in addition to 2; 6. >>> is to move the bits bit to the right n bits, the front complement N 0 */byte a=1; Binary 00000001 byte b=8; Binary 00001000 byte c=127;  Binary 01111111 byte d=-127;//binary 10000001 System.out.println (a&b);  Results 00000000 System.out.println (a|b);   Results 00001001 2^3+2^0=9 System.out.println (~a);  Results 11111110 complement converted to original code =10000001=-1-1=-2 SYSTEM.OUT.PRINTLN (A^B);        Result 00001001 = 9; System.out.println (a<<1);       Results 00000010 2^1=2 System.out.println (c<<1); Results 11111110 =254 System.out.println (a>>1); Results 00000000 =0 System.out.println (c>>1); Results 00111111 =63 System.out.println (d>>1);             Results 11000000 complement original code =10111111=-63-1=-64 SYSTEM.OUT.PRINTLN (d>>>1);//result 01000000 source code/* Note: 1. When the computer is stored, the binary first bit indicates that the sign bit 1 represents a negative number, and 0 represents a positive number 2. When the computer is stored in a complementary fashion, the complement of the positive number is the same as the original code, and the complement of the negative number is removed from the first sign bit after the inverse calculation result and then + 1             .             3. The complement is converted into the original code, which in turn removes the first counter-calculation result and then 1. 4. System.out.println does not have a System.out.println (Byte B) method, the computer automatically converts byte to int and then moves */}}

http://www.verejava.com/?id=16992602784016

Java Base bit operations

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.