Java bit operations and unsigned operations

Source: Internet
Author: User

Tag: Take the inverse BSP nbsp operation to calculate the progressive unsigned system

The computer uses twos complement to perform operations at the bottom.

Calculation rules:

The original code, the inverse code, and the complement of positive numbers are the binary itself.

The original code of negative numbers first calculates the second binary number, and then the highest bit uses 1 for negative numbers, the inverse code is the highest bit of the other bits reversed, complement is on the basis of anti-code for +1 operations.

SYSTEM.OUT.PRINTLN (8 >> 1);//positive number for right shift operation

8 binary is 0000 0000 0000 0000 0000 0000 0000 1000, move right 1 bits get binary 0000 0000 0000 0000 0000 0000 0000 0100, then turn to binary is 4
And so on

/* 1000
* 0100 4 Right shift 1 bit
* 0010 2 Right Shift 2 bit
* 0001 1 Right Shift 3 bit
* 0000 0 Right Shift 4 bit
* */

SYSTEM.OUT.PRINTLN (-8 >> 1);//negative number for right shift operation (right shift high 1)

-8 of the binary is 1000 0000 0000 0000 0000 0000 0000 1000, where the highest 1 indicates a negative number

/*
* 1000 0000 0000 0000 0000 0000 0000 1000 Original Code
* 1111 1111 1111 1111 1111 1111 1111 0111 anti-code
* 1
* 1111 1111 1111 1111 1111 1111 1111 1000 complement
* 1 1111 1111 1111 1111 1111 1111 1111 100-bit operation get the complement and go to the original code
* 1 0000 0000 0000 0000 0000 0000 0000 011
* 1
* 1 0000 0000 0000 0000 0000 0000 0000 100-4 Original Code
* */

System.out.println (8<<2);//positive left shift operation

/*

* 0000 0000 0000 0000 0000 0000 0000 1000 Original Code

* 0000 0000 0000 0000 0000 0000 0010 0000-bit Operation 32

*/

System.out.println ( -8<<2);//negative number for left shift operation (left shift position 0)
/* 1000 0000 0000 0000 0000 0000 0000 1000-8 Original Code
* 1111 1111 1111 1111 1111 1111 1111 0111 anti-code
* 1111 1111 1111 1111 1111 1111 1111 1000 complement
* 11 1111 1111 1111 1111 1111 1111 100,000-bit arithmetic
* 10 0000 0000 0000 0000 0000 0000 011111-bit operation after complement
* 10 0000 0000 0000 0000 0000 0000 100000 Original Code
*-32
* */

System.out.println ( -9>>>2), unsigned operation, high-level 0, low loft
/*
* 1000 0000 0000 0000 0000 0000 0000 1001 Original Code

* 1111 1111 1111 1111 1111 1111 1111 0110 anti-code
* 1111 1111 1111 1111 1111 1111 1111 0111 complement
* 001111 1111 1111 1111 1111 1111 1111 01 shift ( The shift gets a positive number, so the complement is itself )
* */

Java bit operations and unsigned 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.