- #1
Puzzle
- 2
- 0
Hello,
I have a problem where I must get value of a given binary bit in decimal number:
f(decimal_number, bit_position) = bit_value
For example, getting bit values in decimal 14 (binary 1110):
f(14, 0) = 0
f(14, 1) = 1
f(14, 2) = 1
f(14, 3) = 1
In general it's easy:
(14 & 20) >> 0 = 0
(14 & 21) >> 1 = 1
(14 & 22) >> 2 = 1
(14 & 23) >> 3 = 1
& is bitwise AND
>> is bitwise arithmetic shift right
But in specific (technical) application I cannot use bitwise operators :(
So far I managed to get rid of one of them:
(14 & 20) / (20) = 0
(14 & 21) / (21) = 1
(14 & 22) / (22) = 1
(14 & 23) / (23) = 1
But how to get rid of bitwise AND operator?
Specific application limits me to following things to play with:
+, -, *, /, (, ), square root, xy, ex, ln, log, abs, sin, cos, tan, asin, acos, atan
using floating point decimal numbers. And it has to be "single line" - cannot divide solution
to parts, use variables besides given decimal_number or use any if/else etc logic.
It does not have to be "nice" solution, any "hack" will do as long as it works. For example
"helper" constants (specific to bit position) could be used as additional input to the function.
I have a problem where I must get value of a given binary bit in decimal number:
f(decimal_number, bit_position) = bit_value
For example, getting bit values in decimal 14 (binary 1110):
f(14, 0) = 0
f(14, 1) = 1
f(14, 2) = 1
f(14, 3) = 1
In general it's easy:
(14 & 20) >> 0 = 0
(14 & 21) >> 1 = 1
(14 & 22) >> 2 = 1
(14 & 23) >> 3 = 1
& is bitwise AND
>> is bitwise arithmetic shift right
But in specific (technical) application I cannot use bitwise operators :(
So far I managed to get rid of one of them:
(14 & 20) / (20) = 0
(14 & 21) / (21) = 1
(14 & 22) / (22) = 1
(14 & 23) / (23) = 1
But how to get rid of bitwise AND operator?
Specific application limits me to following things to play with:
+, -, *, /, (, ), square root, xy, ex, ln, log, abs, sin, cos, tan, asin, acos, atan
using floating point decimal numbers. And it has to be "single line" - cannot divide solution
to parts, use variables besides given decimal_number or use any if/else etc logic.
It does not have to be "nice" solution, any "hack" will do as long as it works. For example
"helper" constants (specific to bit position) could be used as additional input to the function.
Last edited: