Reputation: 2998
I have to store more than 30 flags(0/1) into a single integer. I can able to create 30 flag masks in Javascript using Bitwise operator like below
var FLAG_1 = 1 << 0; // 1
var FLAG_2 = 1 << 1; // 2
...
...
var FLAG_30 = 1 << 30;
var user1 = FLAG_1 | FLAG_16;
console.log(FLAG_1 & user1); // true
This works fine. But I can't create more than 30 flags because 1 << 31
goes in reverse(compliment or negative number).
Is there any way to do this ?
Upvotes: 0
Views: 554
Reputation: 4793
Yes. Using more than one 32 bit number concatenated together. If you know you need less than 64 say, use two 32 bit numbers. Take the first 32 bits and handle those flags, then separately take the second 32 and handle those.
var test = '0421';
for( var i=0; i < test.length; i += 2 ) {
flags = parseInt( test.substr(i,2), 10 );
console.log(flags);
// do comparisons here
// call function doStuff(i, flags)
}
I'm not sure what your initial input value is. I start off with a string, which can be any quantity of sets of 00 - 31. Each can can be processed by calling a function to do the work, passing the iterator and the flags.
Upvotes: 1