简体   繁体   中英

Design for Bit manipulation

I am creating a plugin. I have a set of different types of values for a record. So, lets say, for each record I have following data

  • type : value
  • A : a1
  • B : b1
  • C : c1
  • D : d1

Now, for each record, I want to keep an applied array -

  • type : applied
  • A : true
  • B : false
  • C : true
  • D : true

Now, I take && of all values of A, B, C, D in applied array, and based on true/false of final AND, I decide if the record is valid or not.

Now, I do not want to keep and array for such a thing. I want to keep 4 bits with 0/1 value and take '&' using bitwise operations which will be faster. I want to know how I can implement such a thing? What will n bit variable look like.

I am creating a plugin, so I will not know in advance if there will be 4 such types. There can be any 'n' such types (n < 32), so that I will have to keep n such bits.

This is what thought so far

  1. I will keep and integer variable with value = 0. (all bits will be 0 initially) I dont know how to keep and integer with 32 bits / n bits.
  2. I will keep a count of how many n types I have
  3. To set to true, somehow I will have to go and set i-th bit to 1, or to 0 if I have to set it to false. I dont know how to do that.
  4. To find AND, I will find the first bit which is equal to 0 in those n bits. If there is a 0, my final value is 0, else its 1.

Language - javascript/jquery

Any example with function signatures only will be helpful

I will keep and integer variable with value = 0. (all bits will be 0 initially) I dont know how to keep and integer with 32 bits / n bits.

JavaScript doesn't have an integer type, but the Number type is guaranteed to be able to represent any integer from −2³¹ to 2³¹−1. You can use bitwise operators on Number 's as if they were 32-bit signed integers.

To set to true, somehow I will have to go and set i-th bit to 1, or to 0 if I have to set it to false. I dont know how to do that.

In order to set i -th bit of x to 1 do this:

x |= (1 << i)

In order to set i -th bit of x to 0 do this:

x &= ~(1 << i)

To find AND, I will find the first bit which is equal to 0 in those n bits. If there is a 0, my final value is 0, else its 1.

There is a simpler way to check if an integer x equals to n ones in binary.

var isAllOnes = x === (1 << n) - 1;

Example for n == 8 :

(1 << n) is 256, or 100000000 in binary (one and 8 zeroes).

(1 << n) - 1 is 255, or 11111111 in binary (8 ones).

Javascript has the usual << operator for left-shifting, so 1 << n will get you a number with the n'th bit set. (This won't work correctly for large n , but n in the range 0 to 31 inclusive should be fine, which you claim in your question is large enough for you.)

You can use the bitwise or operator | to set a bit with this; eg x | 1 << n x | 1 << n will be just like x but with the n th bit set. Similarly ~ and & can be used to turn off a bit: x & ~(1 << n) will be just like x but with the n th bit cleared.

Testing that all the bits are set is fairly easy, too: x === (1 << n)-1 will check whether all the bits from 0 to n-1 are set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM