-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bits.sml
30 lines (30 loc) · 1.07 KB
/
Bits.sml
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
structure Bits =
struct
fun ~>>(x,n) = IntInf.toInt(IntInf.~>>(IntInf.fromInt x,n))
fun <<(x,n) = IntInf.toInt(IntInf.<<((IntInf.fromInt x),n))
fun orb(x,y) = IntInf.toInt(IntInf.orb(IntInf.fromInt x,IntInf.fromInt y))
fun andb(x,y) = IntInf.toInt(IntInf.andb(IntInf.fromInt x,IntInf.fromInt y))
fun xorb(x,y) = IntInf.toInt(IntInf.xorb(IntInf.fromInt x,IntInf.fromInt y))
fun fromTemplate(0,acc,_)= rev acc
| fromTemplate(n,acc,x) = fromTemplate(n-1,Word8.fromInt(x)::acc,~>>(x,0w8))
fun fromInt32(x) = fromTemplate(4,[],x)
fun fromInt64(x) = fromTemplate(8,[],x)
fun toInt xs =
let fun inner([],v) = v
| inner((x::xs),v) = inner(xs,orb(<<(v,0w8),Word8.toInt(x)))
in
inner(rev xs,0)
end
fun toIntOption xs = SOME(toInt xs) handle _ => NONE
end :
sig
val ~>> : int * word -> int
val << : int * word -> int
val andb : int * int -> int
val orb : int * int -> int
val xorb : int * int -> int
val fromInt32 : int -> Word8.word list
val fromInt64 : int -> Word8.word list
val toInt : Word8.word list -> int
val toIntOption : Word8.word list -> int option
end