开发者

Setting last N bits in an array

I'm sure this is fairly simple, however I have a major mental block on it, so I need a little help here!

I have an array of 5 integers, the array is already filled with some data. I want to set the l开发者_如何学编程ast N bits of the array to be random noise.

[int][int][int][int][int]

eg. set last 40 bits

[unchanged][unchanged][unchanged][24 bits of old data followed 8 bits of randomness][all random]

This is largely language agnostic, but I'm working in C# so bonus points for answers in C#


Without any bit-fu in C#:

BitArray ba = new BitArray (originalIntArray);
for (int i = startToReplaceFrom; i < endToReplaceTo; i++)
    ba.Set (i, randomValue);


When you XOR any data with random data, the result is random, so you can do this:

Random random = new Random();
x[x.Length - 2] ^= random.Next(1 << 8);
x[x.Length - 1] = random.Next(1 << 16);
x[x.Length - 1] ^= random.Next(1 << 16) << 16;

For a general solution for any N you can use a loop:

for (int i = 0; i < 40; ++i)
{
    x[x.Length - i / 32 - 1] ^= random.Next(2) << (i % 32);
}

Note that this calls random more times than necessary, but is simple.


In pseudo-Python:

N        = 5   # array size
bits     = 40  # for instance
int_bits = 32  # bits in one integer

i = N
while bits > 0:
    value_bits  = min (bits, int_bits)
    bits       -= value_bits
    mask        = (1 << value_bits) - 1

    i -= 1
    array[i] ^= random () & mask


Int32 is 4 bytes or 32 bits.
So you need the last int, and 8 bits extra.

int lastEightBitsMask = 0x000F + 1;
Random rand = new Random();
arr[arr.Length - 1] = rand.Next();
arr[arr.Length - 2] ^= rand.Next(lastEightBitsMask);

Explanation:
The last element's modification should be pretty clear - if you need the last 40 bits, the last 32 bits are included in that.
The remaining eight bits's modification is bounded above by 0x000F + 1, since rand.Next's argument is an exclusive upper bound, the randoms generated will be no more than that. The remaining bits of the number will stay the same, since 1^0 == 1 and 0^0 == 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜