Skip to content

Instantly share code, notes, and snippets.

@ptpaterson
Last active January 6, 2022 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptpaterson/d5c36650ec296d9ec9ffdd3a4352909a to your computer and use it in GitHub Desktop.
Save ptpaterson/d5c36650ec296d9ec9ffdd3a4352909a to your computer and use it in GitHub Desktop.
Bit shifting in Fauna
// try out some bit shifting!!
// All you really need is to multiply or divide by a power of 2
// Make sure to wrap the `Pow` function in `ToInteger`
/*
* Shift a little bit
*/
Let(
{
int: ToInteger("5188146770730811392"),
shift: 1,
multiplier: ToInteger(Pow(2, Var("shift"))),
shift_left: Multiply(Var("int"), Var("multiplier")),
shift_right: Divide(Var("int"), Var("multiplier")),
},
[
Format("int: 0x%x :: 0b%s :: %d", Var("int"), Call("PrintBinary", Var("int")), Var("int")),
Format("shift_left: 0x%016x :: 0b%s :: %d", Var("shift_left"), Call("PrintBinary", Var("shift_left")), Var("shift_left")),
Format("shift_right: 0x%016x :: 0b%s :: %d", Var("shift_right"), Call("PrintBinary", Var("shift_right")), Var("shift_right")),
]
)
// returns
[
"int: 0x4800000000000000 :: 0b0100100000000000000000000000000000000000000000000000000000000000 :: 5188146770730811392",
"shift_left: 0x9000000000000000 :: 0b0001000000000000000000000000000000000000000000000000000000000000 :: -8070450532247928832",
"shift_right: 0x2400000000000000 :: 0b0010010000000000000000000000000000000000000000000000000000000000 :: 2594073385365405696"
]
/*
* Shift a lot
*/
Let(
{
int: ToInteger("5188146770730811392"),
shift: 59,
multiplier: ToInteger(Pow(2, Var("shift"))),
shift_left: Multiply(Var("int"), Var("multiplier")),
shift_right: Divide(Var("int"), Var("multiplier")),
},
[
Format("int: 0x%x :: 0b%s :: %d", Var("int"), Call("PrintBinary", Var("int")), Var("int")),
Format("shift_left: 0x%016x :: 0b%s :: %d", Var("shift_left"), Call("PrintBinary", Var("shift_left")), Var("shift_left")),
Format("shift_right: 0x%016x :: 0b%s :: %d", Var("shift_right"), Call("PrintBinary", Var("shift_right")), Var("shift_right")),
]
)
// returns
[
"int: 0x4800000000000000 :: 0b0100100000000000000000000000000000000000000000000000000000000000 :: 5188146770730811392",
"shift_left: 0x0000000000000000 :: 0b0000000000000000000000000000000000000000000000000000000000000000 :: 0",
"shift_right: 0x0000000000000009 :: 0b0000000000000000000000000000000000000000000000000000000000001001 :: 9"
]
// UDF to create an array of 2^0 to 2^62
// Fauna uses signed integers, so 2^64 isn't something we can work with
Query(
Lambda(
[],
Map(
[
0,
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,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
],
Lambda("n", ToInteger(Pow(2, Var("n"))))
)
)
)
// Use the PowersOf2 method to print out the number bit by bit
// This would be WAY more efficient and cost effective (Transactional Compute Operations)
// to make a literal list of numbers, but this is easier to write!
// Remember that ints are signed, so we have to handle the signed bit separately
Query(
Lambda(
"int",
Let(
{
number_part: Reduce(
Lambda(
["acc", "val"],
If(
GT(Bitand([Var("int"), Var("val")]), 0),
Concat(["1", Var("acc")]),
Concat(["0", Var("acc")])
)
),
"",
Call("PowersOf2", [])
),
sign_part: If(GTE(Var("int"), 0), "0", "1")
},
Concat([Var("sign_part"), Var("number_part")])
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment