I had some free time and remembered old task from the university: show numeric in binary representation.
In my old days I'd probably write smth with
divs and
mods.
The core of the algorithm was a mathematical transformation: 13 = 1101 = 2
3*1+2
2*1+2
1*0+2
0*1.
Now I just have thought about the problem from other side:
int
main()
{
int n = 13;
for (int i=sizeof(n)*8-1;i>=0;--i)
printf("%d", (n >> i)&1);
return 0;
}
Here we are:
$gcc 1.c -o out -std=c99 -O2
$./out
00000000000000000000000000001101
Just fun =).
No comments:
Post a Comment