Bitwise operators for Java

Source: Internet
Author: User
Tags bitwise bitwise operators

This article refers to: Java bitwise operators

The bitwise operator of Java is used to manipulate a single "bit" (bit) in an integer base data type, which is the generation of a binary bit. And we know that bits are 0 and 1, so the bit operation is the basic operation of the data. If the base type is a numeric value of char, Byte, or short, it is converted to the int type, and then the shift is processed

Bitwise operators for Java

The bitwise operator performs a Boolean algebra operation on the bits corresponding to the two parameters and eventually produces a result. This operation is have with (&), non (~), or (|), XOR, or (^). We know that the units "bit" (bit), that is, the binary bits, are 0 and 1, xor (^) may be more complex points, two operands of the bit, the same result is 0, different results are 1. So the basic logic is like this.

1&1→11&0→0~1→0~0→11|1→11|0→11^0→1 (1 is 01,0 00, then the result is 01, or 1) 1^1→0
The shift operator for Java

The shift operator in Java is nothing more than a displacement of the binary.

The << is to move left, that is, all the binary moves to the left one bit, 0010 0000 << 1 equals 0100 0000

Make >> move right, that is, all binary moves one bit to the right, 0010 0000 >> 1 equals 0001 0000.

You can try the following example, the integer type is also turned into binary to calculate:

class Test{ Public Static void Main(String[]Args){intNumInt1= 3; intNumInt2= -3;System.Out.println(NumInt1<<1);System.Out.println(NumInt1>>1);System.Out.println(NumInt2<<1);System.Out.println(NumInt2>>1);}  }/* The result of the output is as follows (www.breakyizhan.com) 6---> 0000 0011<<1, changed to 0000 01101---> 0000 0011>>1, changed to 0000 0  The 1 behind the 001...1 is topped out, so the result is 1-6--and 1111 1101<<1, which becomes 1111 1010 take the reverse +1= 0000 0110-6-2--1111 1101>>1, Change to 1111 1110 reverse +1= 0000 0010-2 */

Of course, there are Java ternary operators, this part of the function and if-else a bit similar, more details can be seen:
Ternary Operators for Java | Break Easy Station


This article is from the "Technical Blog" blog, please be sure to keep this source http://mrarvin.blog.51cto.com/4551809/1962328

Bitwise operators for 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.