# LaVOZs

The World’s Largest Online Community for Developers

'; bit manipulation - bit wise counter on a single byte javascript - LavOzs.Com

I want to use the bits valued at 2^4-2^8 in a Typed 16Uint array item to use as a binary counter to 16.

``````0000111100000000 ->15
0000111000000000 ->14
0000110100000000 ->13
...
0000000000000000 ->0
``````

is there a simple bit wise operation that would count in binary? my current strategy is to extract the bits as number add one - do some error checking, then set the bits in the original with a&0 and replace that part with a |or mask?

``````cellBinary = iterate(cellBinary, 16, 4);
function iterate(cellBinary, start, length)
{
let number = extractBits(cellBinary, start, length);
if(number < 15)
{
number++;
}
what = eraseIterator(what);
what = what|number;
return what;
}
function extractBits(what, start, length)
{
return ((1 << start ) -1) & (what >> (length - 1));
}
function eraseIterator(what)
{
what&16^1; //also iffy if this will work as intended.
what&32^1; //this is supposed to set 16-128 to 0.
what&64^1;
what&128^1;
return what;
}

``````

is there a better way to accomplish this? note: code is an example, I'm looking for a strategy, not a bug.

You could add the right amount directly: 256, 100000000 in binary.

For example,

``````000000000000 + 100000000 ->
000100000000
001000000000
001100000000
...
111100000000
``````

the bits valued at 2^4-2^8

That would mean this sequence:

``````00000000
00010000
00100000
00110000
...
11110000
``````

Or in other words, add 16.