Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2004, 11:26 AM   #1
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
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++
RosdaHale is offline   Reply With Quote
Old Nov 29th, 2004, 12:32 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
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;
}
Eggbert is offline   Reply With Quote
Old Nov 29th, 2004, 2:34 PM   #3
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
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.
RosdaHale is offline   Reply With Quote
Old Nov 29th, 2004, 4:27 PM   #4
andersRson
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 andersRson is on a distinguished road
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.
andersRson is offline   Reply With Quote
Old Nov 29th, 2004, 5:55 PM   #5
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
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.
RosdaHale is offline   Reply With Quote
Old Nov 29th, 2004, 6:47 PM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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);
}
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 29th, 2004, 7:00 PM   #7
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
Perfect just what i wanted !! thanks a lot
RosdaHale is offline   Reply With Quote
Old Nov 30th, 2004, 10:36 AM   #8
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC