Setting last N bits in an array

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2010-04-21T23:18:11Z Indexed on 2010/04/21 23:23 UTC
Read the original article Hit count: 186

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 last N bits of the array to be random noise.

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

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#

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about bitfoo

  • Easiest way to find the correct kademlia bucket

    as seen on Stack Overflow - Search for 'Stack Overflow'
    In the Kademlia protocol node IDs are 160 bit numbers. Nodes are stored in buckets, bucket 0 stores all the nodes which have the same ID as this node except for the very last bit, bucket 1 stores all the nodes which have the same ID as this node except for the last 2 bits, and so on for all 160 buckets… >>> More

  • Setting last N bits in an array

    as seen on Stack Overflow - Search for 'Stack Overflow'
    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 last N bits of the array to be random noise. [int][int][int][int][int] set last 40 bits [unchanged][unchanged][unchanged][24… >>> More

  • Adding to a bit array

    as seen on Stack Overflow - Search for 'Stack Overflow'
    In my program, I am using BitArrays to represent 160 bit numbers. I want to be able to add, subtract, increment and decrement these numbers, what is the algorithm for doing this? At the moment I'm not interested in multiplication and division, but I might be in the future so bonus points for that… >>> More