最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check that a bit is set (without bitwise operation)? - Stack Overflow

programmeradmin3浏览0评论

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
  • 5 Mathematically, there are no spoons - erm, I mean bits. – Jongware Commented Jul 6, 2015 at 8:46
  • 1 @RoyiNamir, but what wrong with bitwise operation? why you want avoid it? – Grundy Commented Jul 6, 2015 at 8:54
  • :) But still – "the computer translates 44 to its binary representation and just check if bit is set via bitwise operation", no, that is mixing metaphors (or maybe just techniques). "The n'th bit" is a bitwise operation. This is the same as checking the value of "the n'th decimal" in a large decimal number (and for any other base). I'm pretty sure you'll need something like a mod and a div somewhere. – Jongware Commented Jul 6, 2015 at 8:55
  • Any hint why you don't want binary operations? This is the most efficient way of operating on bits (way more efficient than toString or %). Alternately, you can use log, as you obviously know. – Amadan Commented Jul 6, 2015 at 8:56
  • @Amadan I'm building a tinyURL like generator so I have abc..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
 |  Show 2 more comments

3 Answers 3

Reset to default 11

Divide 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

发布评论

评论列表(0)

  1. 暂无评论