File: binarydigits.fut

package info (click to toggle)
haskell-futhark 0.25.32-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,236 kB
  • sloc: haskell: 100,484; ansic: 12,100; python: 3,440; yacc: 785; sh: 561; javascript: 558; lisp: 399; makefile: 272
file content (19 lines) | stat: -rw-r--r-- 507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- https://rosettacode.org/wiki/Binary_digits
--
-- We produce the binary number as a 64-bit integer whose digits are
-- all 0s and 1s - this is because Futhark does not have any way to
-- print, nor strings for that matter.
--
-- ==
-- input { 5 }
-- output { 101i64 }
-- input { 50 }
-- output { 110010i64 }
-- input { 9000 }
-- output { 10001100101000i64 }

def main(x: i32): i64 =
  loop out = 0i64 for i < 32 do
    let digit = (x >> (31-i)) & 1
    let out = (out * 10i64) + i64.i32(digit)
    in out