[H-GEN] Conversion of Integer to Binary

Christopher Biggs chris at stallion.oz.au
Fri Oct 31 00:23:07 EST 1997


Cameron Reeves <cg.reeves at student.qut.edu.au> moved upon the face of the 'Net and spake thusly:

> > Printed out from what?  
> 
> C preferably
>  
> > Shell script?
> > 
> > In bash you can do
> > 
> > echo -e '\xxx' where xxx is an octal number.
> 
> Octal is nice, but I need binary.
> 

Ah, I was barking up the wrong tree.

You want to print binary numbers out?  eg i2b(69) => "100101"

something like

i2b(a){int b=64;char c[65];c[b]=0;for(;a;a/=2)c[--b]=a&1?'1':'0';puts(c+b);}

Not tested, might need tweaking. Just fits in one line. 

This is NOT obfuscated, just terse.  Shorter, anyone?


--
|Christopher Biggs -- chris at stallion.oz.au - PGP mail preferred. MIME OK.|
|Stallion Technologies, Brisbane, Australia.    <http://www.stallion.com>|
|PGP Key ID: C94AA9E1  [37 84 06 6B C0 1C B9 Bd  D5 9D 0F 70 79 31 02 A5]|
\___Use key-server or email me "Subject: sendpgpkey" to get my PGP key___/

-----BEGIN GEEK CODE BLOCK	
Version: 3.1   
GCS>AT d? s:+ a-- C++(++++) UL++++ UC+++$ UA>--- P+@ L++>+++ E+++>++++++   
W+ N++ o K+++ w+>--- O M@ V-- PS(+++) PE@ Y+>++ PGP+++ t++() 5 X++ R tv-   
b+++>++++ DI+++ D G++ e++ h>--- r++>+++ y+ 
------END GEEK CODE BLOCK------


----------------------- HUMBUG General List --------------------------------
echo "unsubscribe general" | mail majordomo at humbug.org.au # To Unsubscribe



More information about the General mailing list