Wrox Programmer Forums conversion
 |
 C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
March 16th, 2004, 09:26 AM
 gbilios Authorized User Join Date: Mar 2004 Posts: 33 Thanks: 0 Thanked 1 Time in 1 Post
conversion

I am writing code to convert a negative binary number to its positive counterpart. The code below doesn't do the conversion properley. Can anyone please help??

#define SIZE 8
void Converter(int binary[])
{
int num[SIZE];

for(count = SIZE - 1; count >= 0; cout--)
{
if(binary[0] = '1'); // is negative
sum[count] = 0; // this making everything to zero
} // end for
}

George N. Bilios
__________________
gbilios
March 16th, 2004, 09:45 AM
 Gert Authorized User Join Date: Jun 2003 Posts: 95 Thanks: 0 Thanked 0 Times in 0 Posts

I don't know if I can help you with your original problem, but I see some potensial problems in you code:

Quote:
 quote:#define SIZE 8
Quote:
 void Converter(int binary[]) { int num[SIZE]; for(count = SIZE - 1; count >= 0; cout--) {   if(binary[0] = '1'); // is negative    sum[count] = 0; // this making everything to zero } // end for }
Where do you use the num array?
The for-loop will go for ever, since count is not decremented (spelling error most likely).
Where is sum decleared?

Gert
March 16th, 2004, 11:11 AM
 gbilios Authorized User Join Date: Mar 2004 Posts: 33 Thanks: 0 Thanked 1 Time in 1 Post

I cant get the code to convert a negative binary number to its positive equivalent, which should add 1 and then convert to decimal. The main issue hgere is that I want the code to be able to turn the zeros into ones and the ones into zeros, egs, 10001100(negative) is converted to 01110011(positive)

#define SIZE 8

int toDecimal(int binary[])
{
int num[SIZE];
int decimal = 0;
int weight = 1;
int num;
int count;
for (count = SIZE -1;count >= 0; count--)
{
if(binary[0] == '0') // is positive, convert to decimal
{
// num = binary % 10;
num = binary[count];
decimal = decimal + num * weight ;
// binary = bianry / 10;
weight = weight * 2;
}
else if (binary[0] == '1')
{
// is negative and must be converted to positive
// equivalent, add 1, and then convert to decimal.
//....
} // end for
return decimal;
} // end function

George N. Bilios
March 17th, 2004, 08:33 PM
 nikolai Friend of Wrox Join Date: Jun 2003 Posts: 836 Thanks: 0 Thanked 0 Times in 0 Posts

Uh, why not just xor with all ones? Why use an array of integers at all? That's an incredible waste of space -- to use an entire integer to contain a 1 or 0.

Also, why are you making comparisons to characters? '1' and '0' are characters. This would make sense if your int array, "binary", is actually an array of characters, but that's not how it's declared.

Next issue: Why is your function called "toDecimal" if what you're really doing is taking the bit inverse? It's misleading.

And what the heck does your "weight" variable have to do with anything? Multiplying by two is the same as shifting a binary number to the left once. Why not use a bit shifting operation instead of multiplication?

Why not just multiply your running sum by 2 each iteration, instead of figuring out just one bit, multiplying that by the appropriate power of 2, then adding it to the running sum?

Let me rephrase the above using code:

int num = 0;
for (count = 0; count < SIZE; ++count)
{
num *= 2; // shift left.
num += (binary[count] == '1')? 0 : 1
}

Isn't that easier than dealing with three different variables?

Take care,

Nik
http://www.bigaction.org/
March 18th, 2004, 05:28 AM
 gbilios Authorized User Join Date: Mar 2004 Posts: 33 Thanks: 0 Thanked 1 Time in 1 Post

the first thing thats suppose to happen in toDecimal is convert a binary number to positive, only if the binary number is a positive. if the binary number was a negative it would convert it to a positive, add 1 (two's complement), then convert to decimal(result).

The original function toDecimal converted a binary number to decimal.
all i did was add code to it so i could enter a two's complement number
and deal with it. obviously my code is wrong.

thanks for helping me.

George N. Bilios
March 18th, 2004, 07:05 AM
 gbilios Authorized User Join Date: Mar 2004 Posts: 33 Thanks: 0 Thanked 1 Time in 1 Post

one point though, in two's complement, a negative binary number is first converted to positive, and then a 1 is added to the rightmost bit. The code you rephrased does not add a one to the rightmost bit of the result:

int count;
int num = 0;
for (count = 0; count < SIZE; ++count)
{
num *= 2; // shift left.
num += (binary[count] == '1')? 0 : 1;
} // end for

George N. Bilios
August 6th, 2004, 06:13 PM
 mehdi62b Friend of Wrox Join Date: Jul 2004 Posts: 623 Thanks: 0 Thanked 1 Time in 1 Post

after five months ;)
yes you are right after above code,write below code
Code:
count=size-1;
while(binary[count])
{
binary[count])=0;
count--;
}
binary[count])=1;
--------------------------------------------
Mehdi.:)

 Similar Threads Thread Thread Starter Forum Replies Last Post conversion... ankur_icfai Flash (all versions) 1 November 9th, 2006 10:38 AM Conversion anukagni Access 2 September 6th, 2006 12:22 AM VB to C# conversion shazia1 VS.NET 2002/2003 3 June 29th, 2005 08:12 PM DataType Conversion using WHERE IN ( ) Colonel Angus SQL Language 3 August 18th, 2004 07:43 AM