# Converting int array of 1's and 0's into true binary

## Recommended Posts

Hi I currently have an array of 32 ints, that are either 1 or 0 and need to be able to convert them into a 32-bit interger with the same binary representation that is in the array. I have used bitwise operations to extract binary from ints and chars by doing a test like:
for (i = 0; i < 32; i++)
{
if(num & 0x80)
A[i] = 1;
else
A[i] = 0;
num = num << 1;
}

representing the string in full binary and storing it in the array of ints, but i dont know how to go back the other way, converting an array like int A[4] = {0,0,1,0}; into the int 2. Can anyone help here? I am using C by the way.

##### Share on other sites
I haven't tested this, but something like this could work I think:

It would be easier if you stored the numbers in there backwards, but whatever. Something like this perhaps?

unsigned char A[NumBits] = {1, 0, 0, 1, 0, 0, 1, 0};int Num = 0;for(int i = 0; i < 8; i++)    Num += (int)A[i] << ((NumBits - 1) - i);

##### Share on other sites
Thanks, I will give it a try. :)

##### Share on other sites
by the way, that for loop should read:

for(int i = 0; i < NumBits; i++)

##### Share on other sites
Right first time!

Works perfect man (well there was a couple of usual warnings :) ), thanks!

##### Share on other sites
No problem, anytime. I'm glad it worked as I hadn't tested it or ever written anything like it before.

Anyways, what warnings did it give you?

##### Share on other sites
Just a slight modification:

unsigned char A[NumBits] = {1, 0, 0, 1, 0, 0, 1, 0};int Num = 0;int bitMask = 1;for(int i = 0; i < NumBits; i++){    bitMask <<= 1;    if(A[i]) Num |= bitMask;}

It's the same but optimized, I think.

##### Share on other sites
unsigned char A[NumBits] = {1, 0, 0, 1, 0, 0, 1, 0};int Num = 0;for(int i = 0; i < NumBits; i++) {    Num <<= 1;    Num |= A[i]; // loses error checking, but should be even faster    // versus "if (A[i]) Num |= 1;"}

##### Share on other sites
Unfortunately not. In that example you are just repeatedly setting the least significant bit.

##### Share on other sites
Quote:
 Original post by MrEvilUnfortunately not. In that example you are just repeatedly setting the least significant bit.

But he's shifting 'num' up by one space every turn, so the bits move from the least significant spot up. Like a printer- the ink thingy stays still but the paper moves.

##### Share on other sites
#include <stdlib.h>long number = 0;int index = 0;char buffer[32];for ( index = 0; index < 32; ++index ){    if ( A[index] )        char[index] = '1';    else        char[index] = '0';}number = strtol( buffer, NULL, 2 );

edit: I asked a similar question very recently, if you store it in a character array originally with '1's and '0's then you can just call strtol();

##### Share on other sites
Here's another flavor in the form of a function.

int arraytoint( int * A ){  int * pointer = A;  int * end = A + 31;  int returnValue = 0;  for( ; pointer <= end; ++pointer )    returnValue |= (*pointer)<<(end-pointer);  return returnValue;}

If A is a pointer to your array of ints, call:
     int x = arraytoint(A);

##### Share on other sites
unsigned long array[32] = {    0,0,0,0,0,1,1,1,    0,1,1,1,1,1,0,0,    1,0,1,1,0,1,0,1,    0,0,1,1,0,0,0,1,};unsigned long bits;bits |= array[0]  << 0;bits |= array[1]  << 1;bits |= array[2]  << 2;bits |= array[3]  << 3;bits |= array[4]  << 4;bits |= array[5]  << 5;bits |= array[6]  << 6;bits |= array[7]  << 7;bits |= array[8]  << 8;bits |= array[9]  << 9;bits |= array[10] << 10;bits |= array[11] << 11;bits |= array[12] << 12;bits |= array[13] << 13;bits |= array[14] << 14;bits |= array[15] << 15;bits |= array[16] << 16;bits |= array[17] << 17;bits |= array[18] << 18;bits |= array[19] << 19;bits |= array[20] << 20;bits |= array[21] << 21;bits |= array[22] << 22;bits |= array[23] << 23;bits |= array[24] << 24;bits |= array[25] << 25;bits |= array[26] << 26;bits |= array[27] << 27;bits |= array[28] << 28;bits |= array[29] << 29;bits |= array[30] << 30;bits |= array[31] << 31;

##### Share on other sites
For more obfuscation, template meta programming

template<int nbBits>
int valueOf(int* A)
{
return A[0] | (valueOf<nbBits-1>(A+1) << 1);
}

template<>
int valueOf<0>(int* A) { return 0; }

Now valueOf<32>(A) would be expanded at compile time to the good expression (if I didnt do any mistake)

##### Share on other sites
Silly me..

For even more obfuscation, just program it in intercal. Maybe this is an example of how not to do it? In 16-Bit that is.

DO .1 <- #1(1) DO FORGET #1DO (1020) NEXTPLEASE DO .3 <- '?.1$#33'~'#0$#65535'DO .3 <- ".3$#1"~#3DO .2 <- !2$,1SUB.1'~'#32767\$#1'DO (3) NEXTPLEASE DO (1) NEXT(3)DO (2) NEXTDO READ OUT .2PLEASE GIVE UP(2) DO RESUME .3DO REMAIN CALM A HUGE ERROR HAS OCCURRED

Where the array is ,1, the counter is .1 and the output is .2, which is outputted at the end. .3 is just a temporary used for the Xor-comparison test.
Remember to compile with the /b switch set, if you want to avoid error 774..

##### Share on other sites
Aww, for a moment there I thought Gamedev.net had syntax highlighting for Intercal. It looks like it's just handling it as Basic though.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628362
• Total Posts
2982266

• 10
• 9
• 13
• 24
• 11