Introduction to Java Bit operators

Source: Internet
Author: User

Bit operation is a unary and two-dollar operation of the bitwise or binary number of the bit-mode in the program design. On many ancient microprocessors, bit operations are slightly faster than addition and subtraction, and the bitwise operations are much faster than the multiplication algorithm. In modern architectures, this is not the case: bit operations are usually the same as the addition operation (still faster than multiplication)

Cons: can only be integer and character type data

Bit operations are mainly for binary computing operations, which are: with & , non-!, or | , xor or ^ , shift left > and right Shift <

1. and &
The following rules apply: The two operands have a bit of 1, the result is 1, otherwise the result is 0, for example:

1&1=1

1&0=0

0&1=0

0&0=0

---

001

101

---------------

001 (Result)

Conclusion 1: two numbers A and B (0<a<b) and its result range are [0,a], including 0 and a

Scenario: HashMap the size of the hashcode and map arrays minus one and manipulate it to get the position angle of the key in the array.

2. xor ^

Conclusion 1: Two The same number is different or the result is 0

Scenario: Storm Arck mechanism that uses this action to determine whether a task is complete.

Others: http://wenwen.m.sogou.com/mq/qudetail.jsp?qid=606323001&w=c%E8%AF%AD%E8%A8%80%E7%9A%84%E8%B4%9F%E5%80% Bc%e7%9c%8b%e4%b8%8d%e6%87%82&g_ut=3

Baidu Encyclopedia: http://baike.baidu.com/view/379209.htm

Introduction to Java Bit 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.