Merge pull request #41373 from volth/bitwise

lib: bitAnd, bitOr, bitXor
This commit is contained in:
Jörg Thalheim 2018-06-03 11:38:56 +01:00 committed by GitHub
commit d036073bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 4 deletions

View File

@ -56,10 +56,10 @@ let
hasAttr head isAttrs isBool isInt isList isString length hasAttr head isAttrs isBool isInt isList isString length
lessThan listToAttrs pathExists readFile replaceStrings seq lessThan listToAttrs pathExists readFile replaceStrings seq
stringLength sub substring tail; stringLength sub substring tail;
inherit (trivial) id const concat or and boolToString mergeAttrs inherit (trivial) id const concat or and bitAnd bitOr bitXor
flip mapNullable inNixShell min max importJSON warn info boolToString mergeAttrs flip mapNullable inNixShell min max
nixpkgsVersion version mod compare splitByAndCompare importJSON warn info nixpkgsVersion version mod compare
functionArgs setFunctionArgs isFunction; splitByAndCompare functionArgs setFunctionArgs isFunction;
inherit (fixedPoints) fix fix' extends composeExtensions inherit (fixedPoints) fix fix' extends composeExtensions
makeExtensible makeExtensibleWithCustomName; makeExtensible makeExtensibleWithCustomName;

View File

@ -45,6 +45,21 @@ runTests {
expected = true; expected = true;
}; };
testBitAnd = {
expr = (bitAnd 3 10);
expected = 2;
};
testBitOr = {
expr = (bitOr 3 10);
expected = 11;
};
testBitXor = {
expr = (bitXor 3 10);
expected = 9;
};
# STRINGS # STRINGS
testConcatMapStrings = { testConcatMapStrings = {

View File

@ -1,4 +1,38 @@
{ lib }: { lib }:
let
zipIntBits = f: x: y:
let
# (intToBits 6) -> [ 0 1 1 ]
intToBits = x:
if x==0 then
[]
else
let
headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1
tailbits = if x < 0 then 9223372036854775807 + ((x+1) / 2) else x / 2; # x >>> 1
in
[headbit] ++ (intToBits tailbits);
# (bitsToInt [ 0 1 1 ]) -> 6
bitsToInt = l:
if l==[] then
0
else
(builtins.head l) + (2 * (bitsToInt (builtins.tail l)));
zipListsWith' = fst: snd:
if fst==[] && snd==[] then
[]
else if fst==[] then
[(f 0 (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd))
else if snd==[] then
[(f (builtins.head fst) 0 )] ++ (zipListsWith' (builtins.tail fst) [] )
else
[(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
in
assert (builtins.isInt x) && (builtins.isInt y);
bitsToInt (zipListsWith' (intToBits x) (intToBits y));
in
rec { rec {
/* The identity function /* The identity function
@ -31,6 +65,15 @@ rec {
/* boolean and */ /* boolean and */
and = x: y: x && y; and = x: y: x && y;
/* bitwise and */
bitAnd = builtins.bitAnd or zipIntBits (a: b: if a==1 && b==1 then 1 else 0);
/* bitwise or */
bitOr = builtins.bitOr or zipIntBits (a: b: if a==1 || b==1 then 1 else 0);
/* bitwise xor */
bitXor = builtins.bitXor or zipIntBits (a: b: if a!=b then 1 else 0);
/* Convert a boolean to a string. /* Convert a boolean to a string.
Note that toString on a bool returns "1" and "". Note that toString on a bool returns "1" and "".
*/ */