Don't understand german? Read or subscribe to my english-only feed.

How geeks celebrate a birthday AKA bin2dec

Far away from Rosetta Code, but that’s what Frank and I came up with when explaining 100000₂:

Guile:

guile <<< \#b100000
guile -c '((@@ (ice-9 format) format) #t "~d~%" #b100000)'

Racket:

racket -e '#b100000'

Ruby:

ruby -e 'puts "100000".to_i(2)'

Python:

python -c 'print int("100000", 2)'

Perl:

perl -e 'print 0b100000'

Zsh:

zsh -c 'print $((2#100000))'
zsh -c 'print $((2#1<<5))'

bc:

echo "ibase=2; 100000" | bc

Clojure:

clojure -e '2r100000'

Scala:

scala -e 'Console.println(Integer.parseInt("100000", 2))'

Octave:

octave -q --eval 'bin2dec("100000")'

AWK:

echo 100000 | awk '{r=0;for(i=1;i<=length;i++){r*=2;r+=(substr($0,i,1)!="0")} print r}'

C:

tcc -run - <<< $'#include <stdlib.h>\nint main() { printf ("%d\\n", strtol("100000", NULL, 2)); return 0;}'

Haskell:

runhaskell <<< 'import Data.Char; main = putStrLn $ show $ foldl1 ((+) . (*2)) $ map (digitToInt) "100000"'

CLISP:

clisp -q -x '#b100000'

One Response to “How geeks celebrate a birthday AKA bin2dec”

  1. Frank Terbeck Says:

    We should have done it like this in guile:

    guile -c '(write #b100000)'