Search code examples
c#arraysbit-manipulation32-bit

Bitwise operation c# array elements(0-1) transferring to integer


we have an int array which contains random 32 numbers(1 and 0's ) and we need to transfer it to an integer number with bitwise how can i do that with bitwise operation

i did it without bitwise with using Math.Pow(2, j) method but how to make it with bitwise ?


Solution

  • If you can use left-shift operator (<<), you can do something like this:

    int number = 0;
    int[] bits = new int[32];
    
    for (int i = 0; i < 32; i++)
        number |= bits[i] << i;
    

    << operator works like Math.Pow(2, j);