Bit shift N bits

Hi quick question regarding bit offset

I have a value in HEX = new byte [] {0x56, 0xAF};

which is 0101 0110 1010 1111

I want the first n bits, example 12

then shift the remaining 4 (16-12) to get 0000 0101 0110 1010 (1386 dec)

I tilt my head around it and make it scalable for n bits.

Thanks!

+1
source share
5 answers

Some time ago, I encoded these two functions, the first shifted the byte [] by the specified number of bits to the left, and the second did the same on the right:

Left shift:

public byte[] ShiftLeft(byte[] value, int bitcount) { byte[] temp = new byte[value.Length]; if (bitcount >= 8) { Array.Copy(value, bitcount / 8, temp, 0, temp.Length - (bitcount / 8)); } else { Array.Copy(value, temp, temp.Length); } if (bitcount % 8 != 0) { for (int i = 0; i < temp.Length; i++) { temp[i] <<= bitcount % 8; if (i < temp.Length - 1) { temp[i] |= (byte)(temp[i + 1] >> 8 - bitcount % 8); } } } return temp; } 

Right shift:

 public byte[] ShiftRight(byte[] value, int bitcount) { byte[] temp = new byte[value.Length]; if (bitcount >= 8) { Array.Copy(value, 0, temp, bitcount / 8, temp.Length - (bitcount / 8)); } else { Array.Copy(value, temp, temp.Length); } if (bitcount % 8 != 0) { for (int i = temp.Length - 1; i >= 0; i--) { temp[i] >>= bitcount % 8; if (i > 0) { temp[i] |= (byte)(temp[i - 1] << 8 - bitcount % 8); } } } return temp; } 

If you need further explanation, please comment on this, then I will edit my post for clarification ...

+6
source

You can use BitArray and then easily copy each bit to the right, starting from the right side.

http://msdn.microsoft.com/en-us/library/system.collections.bitarray_methods.aspx

+1
source

do you want something like ...

 var HEX = new byte[] {0x56, 0xAF}; var bits = new BitArray(HEX); int bitstoShiftRight = 4; for (int i = 0; i < bits.Length; i++) { bits[i] = i < (bits.Length - bitstoShiftRight) ? bits[i + bitstoShiftRight] : false; } bits.CopyTo(HEX, 0); 
+1
source

If you have k full bits and you want the "first" (as in most significant) n bits, you can simply shift kn times to the right. The last kn bits will be deleted, as if "falling" from the end, and the first n will be moved to the least significant side.

0
source

The answer is to use C-like notation, assuming bits_in_byte is the number of bits in a byte defined elsewhere:

 int remove_bits_count= HEX.count*bits_in_byte - bits_to_keep; int remove_bits_in_byte_count= remove_bits_count % bits_in_byte; if (remove_bits_count > 0) { for (int iteration= 0; iteration<min(HEX.count, (bits_to_keep + bits_in_byte - 1)/bits_in_byte); ++iteration) { int write_index= HEX.count - iteration - 1; int read_index_lo= write_index - remove_bits_count/bits_in_byte; if (read_index_lo>=0) { int read_index_hi= read_index_lo - (remove_bits_count + bits_in_byte - 1)/bits_in_byte; HEX[write_index]= (HEX[read_index_lo] >> remove_bits_in_byte_count) | (HEX[read_index_hi] << (bits_in_byte - remove_bits_in_byte_count)); } else { HEX[write_index]= 0; } } } 

Assuming you overwrite the original array, you basically take every byte you write and compute the bytes from which it will receive its shifted bits. You go from the end of the array to the front so as not to overwrite the data you need to read.

0
source

All Articles