1 /* Helper function to implement a fallback for the bit operators
2 `bitAnd`, `bitOr` and `bitXor` on older nix version.
7 # (intToBits 6) -> [ 0 1 1 ]
9 if x == 0 || x == -1 then
13 headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1
14 tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1
16 [headbit] ++ (intToBits tailbits);
18 # (bitsToInt [ 0 1 1 ] 0) -> 6
19 # (bitsToInt [ 0 1 0 ] 1) -> -6
20 bitsToInt = l: signum:
22 (if signum == 0 then 0 else -1)
24 (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum));
26 xsignum = if x < 0 then 1 else 0;
27 ysignum = if y < 0 then 1 else 0;
28 zipListsWith' = fst: snd:
29 if fst==[] && snd==[] then
32 [(f xsignum (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd))
34 [(f (builtins.head fst) ysignum )] ++ (zipListsWith' (builtins.tail fst) [] )
36 [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
38 assert (builtins.isInt x) && (builtins.isInt y);
39 bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum)