2

I have the following:

public int [] PermuteNum(int num)
{
int[] newArray = new int[16];
string strNum = Convert.ToString(num, 2);
int[] bits = strNum.PadLeft(16, '0').Select(c => int.Parse(c.ToString())).ToArray();

for (int i = 0; i < bits.Length; i++)
{

int newBit = P(i);
int NewNum = bits[newBit];
newArray[i] = NewNum;

}

return newArray;
}

How do I convert this array of ones and zeros back to my initial int? The first element is the most significant bit.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • it's not efficient, but you can think of it in terms of a shift and a set bit operation, as you traverse the source array, you take the 1s or 0s, and set the bit 0, then shift the bytes to left –  Aug 16 '13 at 03:08

3 Answers3

2
int result = 0;
for (int i = 0; i < newArray.Length; i++)
{
    result *= 2;
    result += newArray[i];
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thank you! im getting an int back, however im off by 128. example my input value was 4751(0001 0010 1000 1111) after permutation, i have 12505(0011 0001 0101 1001). but result variable has 12633. – user1831140 Nov 17 '12 at 03:25
  • I have edited my answer based on my newest guess, as I am not sure I understand your requirements. I need to understand your bit representation. Is newArray[0] the leftmost bit? – Lajos Arpad Nov 17 '12 at 04:02
  • yes, it is left most bit (i.e. most significant bit), with your modification i get 39564 – user1831140 Nov 17 '12 at 04:25
  • many appologies, the initial logic was right. it was calculation mistake from my part, i was adding 2^7 instead of 2^8 as per my data after permutation (0011 0001 0101 1001) to manually test it, so that explains being exactly 128 off. – user1831140 Nov 17 '12 at 04:56
  • Ok, I've edited my answer to contain the initial solution. Let me know if that's right and good luck in your work. – Lajos Arpad Nov 17 '12 at 16:17
  • I'm glad your problem was solved. If my answer was helpful you can accept it as an answer by checking the check mark. – Lajos Arpad Nov 19 '12 at 16:03
1

You could use bitwise shift operations. Here is peudo code and you can do your research on bitshifting operations:

int accumulator = 0;
  int i = 0;
//this assumes your lowest bit is the first one, reverse order if not
foreach(var bit in bits)
{  
  //when you get to the 3rd bit(i==2), if bit is 1 then it represents 2^2 == 8, 
  //to calculate the value of the bit, isntead of using 2^i power, just shift i places

  //Example: the array 1,0,1 becomes 
  // accumulator +=  2^0 // accumulator now == 1
  // accumulator +=  0^1 // accumulator now == 1
  // accumulator +=  2^2 // accumulator now == 4

  accumulator += (shiftbit by i positions);
  ++i;
}
AaronLS
  • 37,329
  • 20
  • 143
  • 202
  • will this give me: accumulator = 7 or accumulator = 0111? i'm not sure i follow you – user1831140 Nov 17 '12 at 02:43
  • accumulator will contain your integer after the loop runs, so 7 – AaronLS Nov 17 '12 at 03:27
  • Thank you! im getting an int back, however im off, example my input value was 4751(0001 0010 1000 1111) after permutation, i have 12505(0011 0001 0101 1001). but accumulator variable has 39564, same issue as the other solution suggested here. im expect to get 12505 when i convert newArray that contains (0011 0001 0101 1001), i step in to see the values of each array. – user1831140 Nov 17 '12 at 04:48
  • that was the case even after i reverse because i have my most significant bit first. – user1831140 Nov 17 '12 at 04:59
0

Perhaps you can use a BitArray and convert its permutations back to an int using the answer from another question.

Community
  • 1
  • 1
Kit
  • 20,354
  • 4
  • 60
  • 103
  • Yes i have seen this, however i need to do permutation on bit position and with bitarray, i don't know how to do this. taking the number and converting it to string and storing each char of it into an array and then do the bit position permuation was the way i found was easier after doing some googling, please correct me if im wrong! – user1831140 Nov 17 '12 at 02:46