diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-01-20 03:23:37 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-01-20 03:23:37 +0100 |
commit | a966aa2eb460d768245fa6f0a8bea06319b5ecb3 (patch) | |
tree | 8f78e43c8f025bda8fec0e0fdc11597ee630519c /src/algorithms/bits | |
parent | add set_clear (diff) | |
download | algorithms-and-data-structures-a966aa2eb460d768245fa6f0a8bea06319b5ecb3.tar.gz algorithms-and-data-structures-a966aa2eb460d768245fa6f0a8bea06319b5ecb3.tar.bz2 algorithms-and-data-structures-a966aa2eb460d768245fa6f0a8bea06319b5ecb3.tar.xz |
add bit merge
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/algorithms/bits')
-rw-r--r-- | src/algorithms/bits/Bits.java | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/algorithms/bits/Bits.java b/src/algorithms/bits/Bits.java index 327a39e..4a08d05 100644 --- a/src/algorithms/bits/Bits.java +++ b/src/algorithms/bits/Bits.java @@ -48,6 +48,19 @@ public class Bits { return (£{T})((value & ~mask) | (-flag & mask)); } + + /** + * Merge bits from two values + * + * @param zero Integer whose bits should be kept where the mask has zeroes + * @param one Integer whose bits should be kept where the mask has onces + * @param mask The merge mask + * @return {@code (zero & ~mask) | (one & mask)} + */ + public static £{T} merge(£{T} zero, £{T} one, £{T} mask) + { + return (£{T})(zero ^ ((£{T})(zero ^ one) & mask)); + } £>done } |