Filling binary number with leading 0's

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I actually fixed it using 4 statements. Took out about 200 lines of incredibly inefficient if else statements.

void toBinary( int toConvert, FILE *theFile )
{
if( toConvert == 0 )
{
fprintf( theFile, "%d", toConvert );
}

else if( toConvert == 1 )
{
fprintf( theFile, "%d", toConvert );
}

else
{
toBinary( toConvert/2, theFile );
fprintf( theFile, "%d", toConvert%2 );
}
}

That is my recursive function for converting a decimal into binary.

My problem now is that my negative conversion is wrong. I test if the number is positive or negative and use a fillWith variable to tell the function to fill with leading 1's or 0's. I then convert to positive and send it through my function above. Is there anyway for my function above to be modified to accommodate a 2's compliment number?

Thanks guys,
-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
But with that i would have to allocate an additional character array just for the binary conversion instead of simply printing it out if I understand it correctly.

Additionally, I would have to have 15 different arrays of different sizes that for each case right?

It is late, and I might have completely misread/misinterpreted that function.

-Kevin
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Sorry, I'm an idiot. printf will also do what I was thinking of, but print it directly. I was thinking of something like this: printf("%0${n}d", 0) where $n=$numBits-1, but I guess you're using C where strcat isn't so convenient.

However, there is still a much easier way to do what you want (which is to print numBits-1 zeros if (aNumber < 2)): use a loop.

if (aNumber < 2) {
for (int i = 0; i < numBits-1; i++) {
printf("0");
}
}
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: esun
Sorry, I'm an idiot. printf will also do what I was thinking of, but print it directly. I was thinking of something like this: printf("%0${n}d", 0) where $n=$numBits-1, but I guess you're using C where strcat isn't so convenient.

However, there is still a much easier way to do what you want (which is to print numBits-1 zeros if (aNumber < 2)): use a loop.

if (aNumber < 2) {
for (int i = 0; i < numBits-1; i++) {
printf("0");
}
}

Hmm i could have used a for statement. The only problem is, I would have 7 (had to add an 11 bit fill) for loops per the number.

So for 2, 4, 8, 16, 32, 64, ..., 32768 I would need a bunch of for loops (Obviously declining to 1 over time. Would the loop really be any more efficient than just leaving it like it is?

Right now the program is completely working. I read in a file in assembly code. I take that assembly code (Yes all my opcodes and modifiers) and convert it to binary. We are using the LC-3 assembly language, so luckily I only had 19 opcodes to test for. All of this had to be written to a .bin file.

It is completely working, I am just wondering how incredibly inefficient my way was. I have 780 lines of code without comments.

-Kevin
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Gamingphreek
It is completely working, I am just wondering how incredibly inefficient my way was. I have 780 lines of code without comments.
No code that is printing to the console has any real business worrying about efficiency. The reason your code is bad is because its unreadable and unwieldy. You should be using a single loop for this.

Your 'multi-character' constant is because of your '16' -- I'm not sure if you wanted to use "16" or just 16 -- it depends on what type numBits is (I assume int, so therefore just use 16.)

 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
The reason your code is bad is because its unreadable and unwieldy

My code looks very very nice. I take pride in proper formatting. Spaces are dropped when inserting it in here due to HTML formatting.

You should be using a single loop for this.

I can't because I go all the way to 32768. I also have 7 different bit series that I am testing for.

Your 'multi-character' constant is because of your '16' -- I'm not sure if you wanted to use "16" or just 16 -- it depends on what type numBits is (I assume int, so therefore just use 16.)

Yea I forgot to update that I found that out. That was a bone head move on my part. I suppose that is what I get for coding at 415AM.

-Kevin
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,674
146
106
www.neftastic.com
Originally posted by: degibson
Originally posted by: Gamingphreek
It is completely working, I am just wondering how incredibly inefficient my way was. I have 780 lines of code without comments.
No code that is printing to the console has any real business worrying about efficiency. The reason your code is bad is because its unreadable and unwieldy. You should be using a single loop for this.

Wow! That's just wrong on so many levels. I have a console app that has to do it's thing as efficiently as possible - currently it's taking ~2 seconds for it to do what it needs to do and customers are complaining that it's too slow!

And saying the code is bad because it's unreadable... I have no problems reading it, and you shouldn't either. If you do, then I'd be really scared to see the code you put out.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I actually fixed it using 4 statements. Took out about 200 lines of incredibly inefficient if else statements.

void toBinary( int toConvert, FILE *theFile )
{
if( toConvert == 0 )
{
fprintf( theFile, "%d", toConvert );
}

else if( toConvert == 1 )
{
fprintf( theFile, "%d", toConvert );
}

else
{
toBinary( toConvert/2, theFile );
fprintf( theFile, "%d", toConvert%2 );
}
}

That is my recursive function for converting a decimal into binary.

My problem now is that my negative conversion is wrong. I test if the number is positive or negative and use a fillWith variable to tell the function to fill with leading 1's or 0's. I then convert to positive and send it through my function above. Is there anyway for my function above to be modified to accommodate a 2's compliment number?

Thanks guys,
-Kevin
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Originally posted by: SunnyD
Originally posted by: degibson
Originally posted by: Gamingphreek
It is completely working, I am just wondering how incredibly inefficient my way was. I have 780 lines of code without comments.
No code that is printing to the console has any real business worrying about efficiency. The reason your code is bad is because its unreadable and unwieldy. You should be using a single loop for this.

Wow! That's just wrong on so many levels. I have a console app that has to do it's thing as efficiently as possible - currently it's taking ~2 seconds for it to do what it needs to do and customers are complaining that it's too slow!

And saying the code is bad because it's unreadable... I have no problems reading it, and you shouldn't either. If you do, then I'd be really scared to see the code you put out.

I think he's saying that the code that's actually doing the printing really doesn't need to worry about efficiency (unless you're printing pages of characters or something). If it's taking two seconds to actually print to the console, that would be ridiculous (I presume that it's taking 2 seconds to do some processing, which it then prints out rather quickly).

As for the OP, I'm not really sure what your code looks like overall so it's hard to say if there's a more efficient way to do things. There's nothing wrong with lots of if statements (though a switch would be prettier) in general, though.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Ok - my brain is about to implode now. I just did what I should have done all along - USE AN INT ARRAY.

I send the number of bits just like before. I then instantiate an array of 0's to the size of the number of bits.

Then I test for a negative number. If the number is negative I set an isNegative int to 1 and convert the number to positive.

Then I use a while loop to compute the binary representation of that positive integer to replace the 0 with a 1 as I iterate backwards through the int array. Following that, if the isNegative is equal to 1, it enters a for loop which skips the last index in the array and starts inverting the bits from right to left.

I didn't want to make another array - but this way has saved me (Even though I had to go back and erase all my hard work) nearly 350 lines of code.

-Kevin
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
My apologies OP, I don't think I understood (or understand) exactly what it is you're trying to do. It seems to me you're trying to print a fixed-width binary number, but based on the confusion there must be more than that. Anyway, I'm glad you got it sorted out despite all the difficulties.

Originally posted by: SunnyD
Originally posted by: degibson
Originally posted by: Gamingphreek
It is completely working, I am just wondering how incredibly inefficient my way was. I have 780 lines of code without comments.
No code that is printing to the console has any real business worrying about efficiency. The reason your code is bad is because its unreadable and unwieldy. You should be using a single loop for this.

Wow! That's just wrong on so many levels. I have a console app that has to do it's thing as efficiently as possible - currently it's taking ~2 seconds for it to do what it needs to do and customers are complaining that it's too slow!

And saying the code is bad because it's unreadable... I have no problems reading it, and you shouldn't either. If you do, then I'd be really scared to see the code you put out.

I suppose my point on this was a bit subtle. I can see why you're having performance problems.