hello,
Is there any simple way to show output in bin base.
something like....
expected output:Code:int digit = 10; printf("%s",DecToBin(digit));
1010
I use gcc.
hello,
Is there any simple way to show output in bin base.
something like....
expected output:Code:int digit = 10; printf("%s",DecToBin(digit));
1010
I use gcc.
Depends what you mean by simple. For converting an unsigned datatype to binary you could just use bit-shifting to find each individual bit in the variable and stick them in a string of characters.
Something to that effect. I didn't test it, so it might not work entirely properly, but that's the gist of it.Code:void convertToString(unsigned long input, char* output) { int i; for(i = 0; i < 32; i++) //assuming unsigned long is 32 bits. Don't shoot me, Prelude! :D { output[i] = input & (1 << (31 - i)) ? '1' : '0'; } }
**EDIT**
Note: Assuming that an unsigned long is 32 bits is bad programming practice
Last edited by Hunter2; 09-20-2004 at 07:25 PM. Reason: Wasn't sure if it was valid C. Modified a bit..
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>Is there any simple way to show output in bin base.
Sure. So simple that it can be done in one line.
>//assuming unsigned long is 32 bits. Don't shoot me, Prelude!Code:#include <stdio.h> int dtob ( int d ) { return d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0; } int main ( void ) { dtob ( 10 ); return 0; }
Just as long as you mention that it's a bad practice somewhere in your post.
My best code is written with the delete key.
>>Sure. So simple that it can be done in one line.
Woh. So... you wanna comment that line?
>>Just as long as you mention that it's a bad practice somewhere in your post.
Done.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I see what that function does.. but what is the U for?
btw, wonderful use of the ternary operator and recursion.. very impressive.
[edit]
Ok, I also got lost when you or it with '0'.. isnt that 0x30 hex?
hmmmmmmmmmmmmmm.
Last edited by Vicious; 09-20-2004 at 07:33 PM.
What is C++?
...So you get 0x30 or 0x31 ('0' or '1'). You could rewrite it as:Code:putchar ( d & 1U | '0' )
...if it makes more sense to you.Code:putchar ( ( d & 1U ) + '0' )
The U is for Unsigned (I think).
If you understand what you're doing, you're not learning anything.
Ok, I now get what the line does. But why does dtob have to return a value?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
So it knows when to stop recursing?
Is recursing a real word?
What is C++?
probably notIs recursing a real word?
>>So it knows when to stop recursing?
No, it knows that when d == 0...
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Yeah, it returns 0 and quits the function, else it calls it self again (recurses).
I think she did that to show it could be done in one line.
What is C++?
Code:void dtob ( int d ) { d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0; //I think that works... } void dtob ( int d ) { if(d) dtob ( d >> 1U ), putchar ( d & 1U | '0'); //I think that works... } void dtob ( int d ) { int dummy = d ? dtob ( d >> 1U ), putchar ( d & 1U | '0' ) : 0; //That works }
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Ah I see... then um.. I dunno.
I'm sure there is a reason behind it since it's Prelude. Probably some kind of "good habit" lesson in there somewhere.
What is C++?
>>Probably some kind of "good habit" lesson in there somewhere.
I suppose so, even in writing code that breaks just about every readability rule there is Prelude will stick to her good habits
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
thank you all,