Created
May 19, 2020 12:22
-
-
Save mikeacjones/b3c3719d38f2f075b95bd4960e2252b0 to your computer and use it in GitHub Desktop.
Bitwise math helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%dw 2.0 | |
import dw::core::Numbers | |
import dw::core::Strings | |
fun AND(lo: Number, ro: Number) = do { | |
var binary = getBinary(lo, ro) | |
--- | |
Numbers::fromBinary(binary.left map ($ as Number * binary.right[$$] as Number) reduce ($$++$)) | |
} | |
fun OR(lo: Number, ro: Number) = do { | |
var binary = getBinary(lo, ro) | |
--- | |
Numbers::fromBinary(binary.left map (if ($ == "1" or binary.right[$$] == "1") "1" else "0") reduce ($$++$)) | |
} | |
fun XOR(lo: Number, ro: Number) = do { | |
var binary = getBinary(lo, ro) | |
--- | |
Numbers::fromBinary(binary.left map (if ($ == binary.right[$$]) "0" else "1") reduce ($$++$)) | |
} | |
fun getBinary(lo: Number, ro: Number) = do { | |
var loB = Numbers::toBinary(lo) | |
var roB = Numbers::toBinary(ro) | |
var size = max([sizeOf(loB), sizeOf(roB)]) default 0 | |
--- | |
{ | |
left: Strings::leftPad(loB, size, '0') splitBy '', | |
right: Strings::leftPad(roB, size, '0') splitBy '' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment