5 kyu
Common Bit Twiddles
278 of 428user5854572
Description:
In this kata, we are throwing readablity to the wind and are refactoring simple functions to use Bit Twiddling.
Specifications
The functions we will make are:
isEven
isOdd
halfAndFloor
isPowerOfTwo
nthPowerOfTwo
truncate
(note: not in Haskell)abs
You are only allowed to use bitwise / boolean operators (and corresponding assignment operators): !
, &&
, ||
, &
, |
, ^
, ~
, <<
, >>
, >>>
, +
, and -
.
You should not use ?
, :
, *
, /
, %
, if
, else
, for
, while
, do
, switch
, case
, or the object Math
.
###Code Examples
//All inputs are between + or - 2147483647
//Only receives whole numbers
isEven(2) // true
isEven(1) // false
//Only receives whole numbers
isOdd(2) // false
isOdd(1) // true
//Only receives positive whole numbers
halfAndFloor(10) // 5
halfAndFloor(11) // 5
//Only receives positive whole numbers
isPowerOfTwo(256) // true
nthPowerOfTwo(4) // 16
//Receives decimal numbers
truncate(1.1) // 1
//Receives negative / positive whole numbers
abs(-1) // 1
Binary
Bits
Refactoring
Similar Kata:
Stats:
Created | Sep 24, 2014 |
Published | Sep 25, 2014 |
Warriors Trained | 1023 |
Total Skips | 216 |
Total Code Submissions | 2943 |
Total Times Completed | 428 |
JavaScript Completions | 278 |
Haskell Completions | 156 |
Total Stars | 40 |
% of votes with a positive feedback rating | 94% of 86 |
Total "Very Satisfied" Votes | 77 |
Total "Somewhat Satisfied" Votes | 7 |
Total "Not Satisfied" Votes | 2 |