Looking at the int 44 — I need Math.CEIL (log(2) 44)
of binary places to represent 44
.
(answer is 6 places)
6 places :
___ ___ ___ ___ ___ ___
32 16 8 4 2 1
But how can I check that (for example) the bit of 8
is checked or not ?
A simple solution will be do to :
((1<<3) & 44)>0
so this will check if the bit is set.
But please notice that behind the scenes the computer translates 44
to its binary representation and just check if bit is set via bitwise operation.
Another solution is just to build the binary myself via toString(2)
or mod%2
in a loop
Question
Mathematically Via which formula, I can test if n'th
bit is set ?
(I would prefer a non loop operation but pure single math phrase)
Looking at the int 44 — I need Math.CEIL (log(2) 44)
of binary places to represent 44
.
(answer is 6 places)
6 places :
___ ___ ___ ___ ___ ___
32 16 8 4 2 1
But how can I check that (for example) the bit of 8
is checked or not ?
A simple solution will be do to :
((1<<3) & 44)>0
so this will check if the bit is set.
But please notice that behind the scenes the computer translates 44
to its binary representation and just check if bit is set via bitwise operation.
Another solution is just to build the binary myself via toString(2)
or mod%2
in a loop
Question
Mathematically Via which formula, I can test if n'th
bit is set ?
(I would prefer a non loop operation but pure single math phrase)
Share Improve this question edited Jul 6, 2015 at 11:52 Royi Namir asked Jul 6, 2015 at 8:43 Royi NamirRoyi Namir 149k144 gold badges491 silver badges829 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 11Divide by the value of the bit that you want to check
and test if the first bit is set (this can be tested with x mod 2 == 1
)
Math expression:
floor(value/(2^bitPos)) mod 2 = 1
As JS function:
function isSet(value, bitPos) {
var result = Math.floor(value / Math.pow(2, bitPos)) % 2;
return result == 1;
}
Note: bitPos starts with 0 (bit representing the nr 1)
The 'bit' (actually any base) value of an indexed number index in a value val in base base can in general be calculated as
val = 1966;
index = 2;
base = 10;
alert (Math.floor(val/Math.pow(base,index)) % base);
result: 9
val = 44;
index = 3;
base = 2;
alert (Math.floor(val/Math.pow(base,index)) % base);
result: 1
(only 0
and 1
are possible here – the range will always be 0..base-1
).
The combination of Math.floor
(to coerce to an integer in Javascript) and Math.pow
is kind of iffy here. Even in integer range, Math.pow
may generate a floating point number slightly below the expected 'whole' number. Perhaps it is safer to always add a small constant:
alert (Math.floor(0.1+val/Math.pow(base,index)) % base);
You can simply check if the bit
at the position is set to 1
.
function isBitSet(no, index) {
var bin = no.toString(2);
// Convert to Binary
index = bin.length - index;
// Reverse the index, start from right to left
return bin[index] == 1;
}
isBitSet(44, 2); // Check if second bit is set from left
DEMO
toString
or%
). Alternately, you can uselog
, as you obviously know. – Amadan Commented Jul 6, 2015 at 8:56abc..ABC...0..9
which is 26+26+10 which is 62 options so it's base 62. now , after I have the number to encode (44) and I have the number of places . but now iterating through each place I need to knowif there's vlue and what is the value – Royi Namir Commented Jul 6, 2015 at 9:01