summaryrefslogtreecommitdiff
path: root/ipl/procs/bitint.icn
blob: 51e73f76432d4cf1726dd3718c94467a03ad4dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
############################################################################
#
#	File:     bitint.icn
#
#	Subject:  Procedures to convert integers and bit strings
#
#	Author:   Ralph E. Griswold
#
#	Date:     May 25, 1994
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#   int2bit(i) produces a string with the bit representation of i.
#
#   bit2int(s) produces an integer corresponding to the bit representation i.
#
############################################################################

procedure int2bit(i)
   local s, sign

   if i = 0 then return 0
   if i < 0 then {
      sign := "-"
      i := -i
      }
   else sign := ""
   s := ""
   while i > 0 do {
      s := (i % 2) || s
      i /:= 2
      }
   return sign || s
end

procedure bit2int(s)
   if s[1] == "-" then return "-" || integer("2r" || s[2:0])
   else return integer("2r" || s)
end