bit-masks

Why does the BitConverter return Bytes and how can I get the bits then?

As input I get an int (well, actually a string I should convert to an int). This int should be converted to bits. For each bit position that has a 1, I should get the position. In my database, I want all records that have an int value field that has this position as value. I currently have the following naive code that should ask my enti...

Writing a function: short GetBits(short data, int p, int n)

I am writing a function short getBits(short data, int p, int n) I have tried: public static short getBits(short data, int p, int n) { short bitmask = (short) ((~0 << (16 -n)) >>> p); short returnVal = (short) ((bitmask & data) >>> (16 - n)); return returnVal; } This works for getBits( (short) 0x7000, 0, 4) but if I were to replac...