Java complement table and displacement operators

Source: Internet
Author: User

In Java, data is stored in binary form.

But how is the data that we see 10 binary?

Because the ToString () method is called automatically before the Java presentation

Here, for example, 4-bit 2, 4-bit 2-binary can only represent 16 numbers, or 0-15. But the nature of the number is not only positive, but also negative, how to say?

So Java is represented in the following way, see:

If it is calculated according to the above complement, then the result of 7+1 is the result of -8,7+2-9;

The underlying storage is binary, but the corresponding number is displayed to the user

7 with the binary representation of 0111, plus 1 for the 1000,java bottom is the binary code is 1000, but the display is 1000 corresponding to the number, 8;

Overflow is like this, has been circulating. Again such as 7+16, the result is still 7;

This is why (2^31-1) + 1 = -2^31

A number n n = n + (max+1) is equivalent to a circle around and back to this number!

n = ~n+1; (except Max,min,0,-1)

For the displacement operator, see the following code:

public class Binary {public static void main (string[] args) {//converted to 2 binary, with integer.tobinarystring ()//converted to 16 binary, With integer.tohexstring () int i = 89; System.out.println (integer.tobinarystring (i)),//parseint () int a = Integer.parseint ("232"); System.out.println (a); System.out.println (Integer.tobinarystring (a)); System.out.println (Integer.tohexstring (a));/** * ~: 0-->1 1-->0 * NOTE: 00000001---> 11111110 */system.out.pr Intln (~a);  System.out.println (integer.tohexstring (~a)); int n;int m;/** * | or * * 0 | 0 = 0 * 0 | 1 = 1 * 1 | 0 = 1 * 1 | 1 = 0 * * Commonly used for data stitching *///n = 00000000 00000000 00000000 10100010//m = 00000000 00000000 10111000 00000000//i = 00000    00000000 10111000 10100010 n = 0xa2;    m = 0xb800; i = n | M    0XB8A2;    System.out.println (integer.tohexstring (i)); /** * & with * * 0 & 0 = 0 * 0 & 1 = 0 * 1 & 0 = 0 * 1 & 1 = 1 * * Common terms and tear Sub-intercept Data * * n = 00000000 00000000 00110100 10110101 * m = 00000000 00000000 00000000 11111111 * i = 00000000 00000000 00000000 10110101 */n = 0x34b5;    m = 0xFF; i = n & M;    0xb5;        System.out.println (integer.tohexstring (i)); /** * >>> Logic Right shift * n = 01010001 11000000 00000010 01011111 * n>>>1 = 001010001 11000000 00 000010 0101111 1 (removed after right shift, high fill 0) * * n = 10100000 10110101 00101100 00100010 * C1 = 11111111 00000000 00000000  00000000 * C2 = 00000000 11111111 00000000 00000000 * c3 = 00000000 00000000 11111111 00000000 * C4 = 00000000    00000000 00000000 11111111 */int b1,b2,b3,b4;    n = 0xa0b52c22;    Intercept//split into Byte B1 = (n>>24) & 0xFF;    B2 = (n>>16) & 0xFF;    B3 = (n>>8) & 0xFF;    B4 = n & 0xff;    System.out.println (Integer.tohexstring (B1));    System.out.println (integer.tohexstring (B2));    System.out.println (Integer.tohexstring (B3));    System.out.println (integer.tohexstring (B4)); Stitching//byte Group Synthesis int n = B4 | (B3&LT;&LT;8) | (b2<<16) |    (b1<<24);    System.out.println (integer.tohexstring (n)); /** * &GT;&GT;: Positive when the high point is 0 >> 0 * Negative number when the high point is 1 >> 1 * With 0 must be positive, starting with 1 is negative *//** * &L     t;<: Move left * 10 binary move one equivalent to the source data *10 * 2 in a mobile equivalent to the source data * * 2<<1 = 2*2 * 2<<2 = 2*4 = (2*2) * 2<<3 = 2*8 = (2*2*2) * 20<<1 = 20*2 * 20<<2 = 20*4 = 20* (2*2) * * Displacement N, equivalent to 2  N Power */System.out.println (2&LT;&LT;3); 2*2*2*2=16 System.out.println (50<<4); 50*2*2*2*2=800}}

Java complement table and displacement operators

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.