4

I have another late night brain-dead question. It's probably simple to do but you know how it is after 8 hours+ of programming then reaching then end of a deadline. :)

Here is my question..

I have a boolean array of length 10 which is logically divided into seperate pieces to hold 4 different integer bit arrays (as booleans). Here is the target array:

bool[] myArray = new bool[10]; 

And here are the 4 integers I would like to insert:

int value1 = 3;  // 2 bits, myArray[0-1], 11
int value2 = 12; // 4 bits, myArray[2-5], 1100
int value3 = 2;  // 2 bits, myArray[6-7], 10
int value4 = 1;  // 2 bits, myArray[8-9], 01

myArray should end up looking like the following (note that the first element is position 0):

{(T,T),(T,T,F,F),(T,F),(F,T)}

Then ultimately what I want to do is convert myArray to an int value:

0x1111001001 = 969

Perhaps there is an even better way of doing this without having to use booleans? So let me rephrase my question in a more general sense:

How do I concatenate N int values into a target int?

Thanks!

4
  • 1
    Not an answer, but I think that the better tool for you is the BitArray class Commented Dec 2, 2012 at 9:50
  • I can't understand why value4 (the 1) contributes with two bits, 01. I think it should give only one bit? If every int value contributed with 0 to 31 bits depending on its magnitude, it could be fun to code. Commented Dec 2, 2012 at 12:50
  • @Jeppe even though value4 is 1 it requires 2 bits because it can be 0 through 3. In my example I simply used the value 1. Commented Dec 2, 2012 at 18:51
  • @JanTacci In that case my answer below won't be the right thing for you. For my answer does not hard-code the bit lengths 2;4;2;2. Instead it always takes the shortest possible bit length. So it doesn't return a fixed-length bit pattern (10=2+4+2+2) like you require. Commented Dec 2, 2012 at 19:52

2 Answers 2

1

Normally, the common way to combine bits from several numbers into one is this (using your values and bitlengths as examples):

var result = value1 | (value2 << 2) | (value3 << 6) | (value4 << 8);

However, the number you printed at the end of your question has the numbers back to front. If that’s what you wanted, it’d be:

var result = value4 | (value3 << 2) | (value2 << 4) | (value1 << 8);

Of course this assumes that the code knows the desired bitlength of each value. If you don’t know the bitlength at compile-time, then you have to keep track of it at runtime, otherwise a single int like 1 won’t tell your code how many bits to use from it.

Sign up to request clarification or add additional context in comments.

6 Comments

I do know the desired bit lenght for each element in the target!
No your answer is quite clear. Did I say something that indicated otherwise?
@JanTacci: Thank you. :) And no, never mind, I just misread your comment :)
@JanTacci But this doesn't solve the problem as I understood it. For each int value, take its binary representation, remove leading zeros, and then concatenate with the next item.
@JeppeStigNielsen: value4 in his example has a leading zero that he wants to retain.
|
0

Here's an attempt to write code that concatenates the binaray representation of integers where there's no leading zeros in the binary representations.

static int Concat(int high, int low)
{
  // find location i of most significant bit of "low"
  int i;
  for (i = 31; i > -1; --i)
    if ((low & (1 << i)) != 0)
      break;

  if (i == 31)
    return low;
  return low & (high << (i + 1));
}

static int ConcatMany(IEnumerable<int> values)
{
  return values.Aggregate(Concat);
}

This should be used like this:

int concatOfTwoNumbers = Concat(3, 12);

var list = new List<int> { 3, 12, 2, 1, };
int concatOfList = ConcatMany(list);

Of course, if there's more than 32 bits in the concatenation, the most significant bits "fall off" to the left. To use with 64-bit integers, change high and low to long, and change both 31 to 63.

1 Comment

@JanTacci I realize that my answer is not what you want. See my new comment to your original question. But it was fun to solve "my" problem even if it was just a misunderstanding of your question. Also note with my "concatenation" you cannot reconstruct the individual values from the "sum" because there's no way to tell where the "borders" between the individual "parts" (values) are once the concatenation has been performed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.