Challenge
1
Name:
Anonymous
2009-06-15 14:23
Write a C program which outputs binary values of all possible bytes(0-255) as e.g. '00001011\n' in the least space.
2
Name:
Anonymous
2009-06-15 14:25
Do your own homework.
3
Name:
Anonymous
2009-06-15 15:11
for (uint8_t byte = 0;; ++byte) {
for (int i = 0; i < 8; ++i) putchar(byte & (0x80 >> i) ? '1' : '0');
putchar('\n');
if (byte == 0xFF) goto end;
} end:;
4
Name:
Anonymous
2009-06-15 15:24
mapM_ putStrLn $ sequence $ replicate 8 ['0','1']
5
Name:
Anonymous
2009-06-15 15:27
for (uint8_t byte = 0;; ++byte) {
if (byte == 0xFF) goto end;
Am I missing something here?
6
Name:
Anonymous
2009-06-15 15:40
for(char i=0;i<256;i++){for(char j=0;i<8;j++){printf("%i",i&1);i>>1 }printf("\n");}
7
Name:
Anonymous
2009-06-15 15:41
>>6
for(char i=0;i<256;i++){for(char j=0;i<8;j++){printf("%i",i&1);i>>=1}printf("\n");}
Forgot an
=.
8
Name:
Anonymous
2009-06-15 15:46
int i,b=256;while(b--){i=8;while(i--)putchar(b&(1<<i)?'1':'0');puts("");}
9
Name:
Anonymous
2009-06-15 16:08
>>8
Nice!
but shouldn't that be
i=7? I mean
1<<1 doesn't result in
1. Also use
48 and
49 instead of
0 and
1 for further shortening.
10
Name:
Anonymous
2009-06-15 16:32
11
Name:
Anonymous
2009-06-15 16:34
>>9
Ah yes. Also I'm not feeling the
putchar and
puts calls. Does anyone know any other (space-optimised) ways of doing that?
12
Name:
Anonymous
2009-06-15 16:41
char i = 0;
while( i++ < 255 ) printf( "%b\n", i );
return 0;
13
Name:
Anonymous
2009-06-15 17:45
256.times{|i|puts Array.new(8){|n|i[n]}.reverse.join}
14
Name:
Anonymous
2009-06-15 18:02
(dotimes (i 256) (format t "~B~%" i))
15
Name:
Anonymous
2009-06-15 18:10
srsly u guys
char i;main(){do putchar(i);while(++i);}
>>4
sequence $ replicate
Use
replicateM!
putStrLn . unlines $ replicateM 8 "01"
17
Name:
Anonymous
2009-06-15 19:04
say unpack"B*",chr for 0..255
18
Name:
Anonymous
2009-06-15 19:11
: bits 256 0 do i 7 for dup i rshift 1 and '0 + emit next cr loop ;
19
Name:
Anonymous
2009-06-15 19:27
>>16
I still don't know a lot :/
why not
mapM_ putStrLn $ replicateM 8 "01" then?
20
Name:
Anonymous
2009-06-15 19:32
>>16
You can do that too, but some prefer to do as much as possible in purely functional code.
21
Name:
Anonymous
2009-06-15 19:33
def s(n,j):
if j+1:print n>>j&1,;s(n,j-1)
for i in range(256):s(i,7);print
Where's your FORCED now‽
22
Name:
>>18
2009-06-15 21:28
Corrected:
: bits 256 0 do 7 for j i rshift 1 and '0 + emit next cr loop ;
23
Name:
Anonymous
2009-06-15 21:32
>>20
PROTIP: replicateM is purely functional.
24
Name:
Anonymous
2009-06-16 0:19
Isn't there a non-alphabet operator replacement for sequence?
25
Name:
Anonymous
2009-06-16 18:04
>>18,22
What language is that? Factor, Forth?
26
Name:
Expert Haskell Programmer
2009-06-16 19:49
>>24
--There is now
($&%@#) = sequence
27
Name:
Expert Haskell Programmer
2009-06-16 21:45
y = sin x lol guys im so much better than lisp
28
Name:
Anonymous
2009-06-17 17:14
char i = 1;
while( i++ ) printf( "%b\n", i );
return 0;
29
Name:
Anonymous
2009-06-17 17:28
>>28
missing
0 , also there is no
%b .
30
Name:
Anonymous
2009-06-17 17:45
31
Name:
Anonymous
2009-06-18 19:01
>>25
That language is, indeed, Forth.
32
Name:
Anonymous
2009-06-18 19:57
(echo obase=2; tr '\0' '\n' < /dev/zero | head -n 256 | nl -v 0 -b a) | bc
or, if you have seq:
(echo obase=2; seq 0 255) | bc
33
Name:
Anonymous
2009-06-18 20:17
[a:b:c:d:e:f:g:[h] | a <- i, b <- i, c <- i, d <- i, e <- i, f <- i, g <- i, h <- i]
where i = "01"
34
Name:
Anonymous
2009-06-18 20:45
You guys are forgetting to make the fon't smaller. That will take up even less space.
35
Name:
Anonymous
2009-06-18 20:56
>>34
echo -e "main = let i = \"01\" in putStrLn $ \"<span style='font-size:0.1px'>\" ++ unlines [a:b:c:d:e:f:g:[h] | a <- i, b <- i, c <- i, d <- i, e <- i, f <- i, g <- i, h <- i] ++ \"</span>\"" | runghc | w3m
36
Name:
Anonymous
2009-06-19 6:40
37
Name:
Anonymous
2009-06-19 9:30
>>33
PROTIP: [a,b,c,d,e,f,g,h]
38
Name:
Anonymous
2009-06-19 12:34
print '00001011\n'
39
Name:
Anonymous
2009-06-19 13:12
>>33
You do know that's exactly what
replicateM 8 "01" does?
40
Name:
Anonymous
2009-06-19 19:32
>>39
I thought I understood monads and suddenly you made this post.
And I feel kind of bad about it :(
Newer Posts