![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 15
Rep Power: 0
![]() |
I want to convert an array which I am using to store a binary numbers (eg, Remainders[7]={0} (array gets filled during the program) into a whole integer number. I was told I could use the & sign to stick things on to the end of the integer but cant work it out.
(example of what i want to do) for(x=0;x<7;x++) { IntResult=IntResult&Remainders[x]; } So for example the array 0,1,0,1,0,1,1 would become 0101011 in the integer. Thanks By the way using Visual C++ |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
You might find this a bit easier to work with:
#include <stdio.h>
int main ( void )
{
int bits[] = { 0, 0, 1, 0, 1, 0, 1, 1 };
int result = 0;
int i;
for ( i = 0; i < 8; i++ )
result = 2 * result + bits[i];
printf ( "%d\n", result );
return 0;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 15
Rep Power: 0
![]() |
This does not work. What i want is a direct copy of the array {0,0,1,0,1,0,1,1} stored in to an integer (00101011), This just adds the one's together and multiplys it by 2 and gives the answer.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
how about this?
#include <stdio.h>
int main ( void )
{
int bits[] = { 0, 0, 1, 0, 1, 0, 1, 1 };
int result = 0;
int i;
for ( i = 0; i < 8; i++ )
result = (2 * result) | bits[i];
printf ( "%d\n", result );
return 0;
}The same code as above, just replaced the '+' with a bitwise or, '|'. The original should have worked though - are you sure you got it right? Both versions should do what you want - At each iteration, the result variable is multiplied by 2(ie all bits shifted one step to the left) and the result is or'ed with the bit you want - or you add the bit, which will really do the same thing. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2004
Posts: 15
Rep Power: 0
![]() |
Yes, i am copying and pasting the code, but still dont work, it comes out with the answer 43 on the screen, but it should come out with the contents of the array only as an integer (00101011)
This example is converting the binary to the decemal equivelent which i dont want, I still want the binary version only as an integer and not an array. All i want is the array turned in to an integer. |
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Only problem is this will drop off leading 0's.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int bits[] = { 0, 0, 1, 0, 1, 0, 1, 1 };
char cBits[8];
int i;
for(i = 0; i<8; i++)
{
cBits[i] = bits[i]+0x30;
}
int converted =atoi(cBits);
printf("\n%d", converted);
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2004
Posts: 15
Rep Power: 0
![]() |
Perfect just what i wanted !! thanks a lot
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>What i want is a direct copy of the array {0,0,1,0,1,0,1,1} stored in to an integer (00101011),
My appologies. I misunderstood what you wanted. Though you should use a long integer because some inputs can overflow an int on certain machines. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|