Bitwise computing applications and tools

Source: Internet
Author: User
Tags bitset

Bitwise computing applications and tools

Sometimes efficiency is required, instead of some arithmetic operations.

  • Quote: a> n <=> a/2 ^ n
  • Product: a <n <=> a * 2 ^ n
  • Remainder: a & (1 <n)-1) <=> a % 2 ^ n
  • Parity: a & 1 = 1 <=> a % 2 = 1

 

Generally, 1 indicates the selected or enabled items, and 0 indicates that the items are not selected or closed.

  • Obtain all items: (1 <n)-1
  • Get specified item: 1 <idx
  • Obtain inverse options :~ A
  • Enable specified item: a | = B
  • Merge two options: a | B
  • Disable the specified item: a & = ~ B
    Note: items in a are 0 when 1 is met, and remain unchanged when 0 is encountered.
  • View public items: a & B
  • Retain specified items: a & = B
    Note: items in a are 0 and remain unchanged when 0 is met.
  • Switch the specified item: a ^ = B
    Note: items in a remain unchanged when 0 is encountered. Changes from 0 to when 1 is encountered.
  • View change item: a1 ^ a2
    Note: a1 is the change before and after a2 is the change, a1 ^ a2 can calculate the change items before and after.
  • View unchanged items :~ (A1 ^ a2) = ~ A1 ^ a2 = a1 ^ ~ A2

 

Output combination, as shown in the following example.

public static void main(String[] args) {    char[] chars = "abcd".toCharArray();    int len = chars.length;        int bits = (1 << len) - 1;    for (int i = bits; i > 0 ; i = (i - 1) & bits) {        System.out.printf("%" + len + "s:", Integer.toBinaryString(i));        for(int j = 0; j < len; j++) {            if(((i >> j) & 1) == 1)                System.out.print(chars[j]);        }        System.out.println();    }}

Output result:

1111:abcd1110:bcd1101:acd1100:cd1011:abd1010:bd1001:ad1000:d 111:abc 110:bc 101:ac 100:c  11:ab  10:b   1:a

 

Precautions for Displacement:

  • > The value is shifted to the right with a symbol. That is, if a positive number is used, the value is 0 on the left, and if a negative number is used, the value is 1 on the left.
  • >>> It is an unsigned right shift, that is, it always fills 0 on the left.

 

Long. bitCount (long), Integer. bitCount (int): calculates the number of 1.

BitSet: indicates the number of storage digits that can be longer than long.

  • Int cardinality (), calculates the number of 1.
  • And (BitSet), and operation, that is, retain the specified item, a & = B. Boolean intersects (BitSet) to determine whether a public item exists.
  • Or (BitSet), or operation, that is, enabling the specified item, a | = B.
  • Xor (BitSet), exclusive or operation, switch to a specified item, that is, a ^ = B. Flip (...), switch the specified item, that is, a ^ = B.
  • AndNot (BitSet), and non-operation, that is, close the target item, a & = ~ B.
  • Int length () to obtain the length. Boolean isEmpty () to determine whether the length is 0.
  • Set (...) to enable the specified item. Clear (...) to close the specified item. Set (..., boolean), set the value of the specified item. Clear (), close all items, and clear, that is, the length is reset to 0.
  • Boolean get (int) to get the switch value of the specified item.
  • Int previussetbit (int)/int nextSetBit (int): searches for the index of the previous or next enable item from the specified position.
  • Int previousClearBit (int)/int nextClearBit (int): searches for the index of the previous/next closed item from the specified position.
  • BitSet get (int, int): truncates a subset.
  • Construction: s + valueOf (ByteBuffer/byte []/LongBuffer/long [])
  • Conversion: toLongArray ()/toByteArray ()

Reference: http://blog.csdn.net/u011039332/article/details/49967559

 

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.