40319

Dynamic Bit Shifting / Unshifting

Question:

i'd like to store multiple values of blocks into an integer in the same way as i can convert an ip into an int. My problem is that i need to do this for n blocks, not only 4. Also i need to specifiy the maximum value of each block, which is allways the same for all blocks.

So, for the Example below, if i'd like to store the whole IP range my BlockCount is 4 and my BlockSize is 255, which is the max value of each block. But it seems not to work if i lower my BlockSize and/or BlockCount.

This is my first try, but it's not working correctly:

const int BlockSize = 100; const int BlockCount = 3; int shiftedValues = Shift(BlockSize, BlockCount); for (int shiftedValue = 1; shiftedValue <= shiftedValues; shiftedValue++) { for (int index = 0; index <= BlockCount; index++) { int blockValue = Unshift(index, shiftedValue); } } private static int Shift(int blockSize, int blockCount) { int result = 0; for (int i = 0; i < blockCount; i++) { result += ( blockSize << 8 * i ); } return result; } private static int Unshift(int blockIndex, int shiftedValue) { return ( shiftedValue >> ( blockIndex * 8 ) ) & 0xFF; }

Answer1:

The code above is my solution, it's pretty simple code, but feel free to ask clarification about it.

class Program { static void Main(string[] args) { int [] items = { 150 , 78 , 44 } ; int x = Program.Pack ( items , 150 ) ; int [] unpacked = Program.UnPack ( x , 150 , 3 ) ; } public static int Pack ( int[] blocks , int blockSize ) { int size = (int)Math.Ceiling(Math.Log(blockSize, 2)); int len = size * blocks.Length; if (len > 32) throw new Exception("Int Limit Exceeded"); if ( blocks.Any ( x => x > blockSize ) ) throw new Exception ( "There are some blocks that exceede the maximum block size" ); List<bool> bools = new List<bool>(); bools = bools.InitBoolArray(32); int i = 0 ; foreach (int block in blocks) { BitArray temp = block.ToBinary().Take(size); for ( int j = 0 ; j < size ; i++ , j++ ) bools[i] = temp.Get(j); } return (new BitArray ( bools.ToArray() ) ).ToNumeral() ; } public static int[] UnPack ( int entry , int blockSize , int blockCount ) { BitArray number = entry.ToBinary(); int size = (int)Math.Ceiling(Math.Log(blockSize, 2)); if (size > 32) throw new Exception("Int Limit Exceeded"); List<int> result = new List<int>(); for (int i = 0; i < blockCount; i++) { BitArray temp = number.Take(size); number = number.Shift (size ); result.Add(temp.FitSize(32).ToNumeral()); } return result.ToArray() ; } }

There extension method used

public static class BinaryConverter { public static BitArray ToBinary(this int numeral) { return new BitArray(new[] { numeral }); } public static int ToNumeral(this BitArray binary) { if (binary == null) throw new ArgumentNullException("binary"); if (binary.Length > 32) throw new ArgumentException("must be at most 32 bits long"); var result = new int[1]; binary.CopyTo(result, 0); return result[0]; } public static BitArray Take (this BitArray current, int length ) { if (current.Length < length) throw new Exception("Invalid length parameter"); List<bool> taken = new List<bool>(); for (int i = 0; i < length; i++) taken.Add(current.Get(i)); return new BitArray(taken.ToArray()); } public static BitArray Shift (this BitArray current, int length ) { if (current.Length < length) throw new Exception("Invalid length parameter"); List<bool> shifted = new List<bool>(); for (int i = 0; i < current.Length - length; i++) shifted.Add(current.Get(length + i)); return new BitArray(shifted.ToArray()); } public static BitArray FitSize (this BitArray current, int size) { List<bool> bools = new List<bool>() ; bools = bools.InitBoolArray(size); for (int i = 0; i < current.Count; i++) bools[i] = current.Get(i) ; return new BitArray(bools.ToArray()); } public static List<bool> InitBoolArray(this List<bool> current, int size) { List<bool> bools = new List<bool> (); for (int i = 0; i < size; i++) bools.Add(false); return bools ; } }

Recommend

  • Adding javascript function to jMeter using jsr223
  • Rijndael managed: plaintext length detction
  • how do disable disk cache in c# invoke win32 CreateFile api with FILE_FLAG_NO_BUFFERING
  • Meteor helpers not available in Angular template
  • Linq Objects Group By & Sum
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • How reduce the height of an mschart by breaking up the y-axis
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Apache 2.4 - remove | delete | uninstall
  • php design question - will a Helper help here?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?