1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@altruios much easier: if (value < 0xF0) value += 16
Or 0xF00 and 256 for the version with 8 padding bits, it's not really clear to me which one of them you meant
so: that makes a lot of sense. i'm not used to working with bitwise stuff, that seems like a really good way to think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.