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

javascript - Determining Powers of 2? - Stack Overflow

programmeradmin2浏览0评论

I am creating a simple bracket system and I need a way to check if there are a correct number of teams, OR if my program needs to pensate for bye rounds.

Right now, I am checking for "powers of two" with this function:

function validBracket(data) {
    var x = data.teams.length;
    return ((x != 0) && !(x & (x - 1)));
}

This works pretty well, but I am needing to know how many Bye rounds to add. For instance, if I had 16 teams, I would not need to add anymore teams. However, if I had 12 teams, I would need the first 4 teams to get a bye round.

How can I calculate number of bye rounds to add to my bracket? And would hard-coding an array of powers of two be better?

In pseudo code, something like this is what i was thinking of:

if(validateBracket(data)) {
    // Valid number of teams (power of two). Keep going.
} else {
    var byeRounds = calculateByeRounds();
}

NOTE: I would rather not use an array of powers of two like below:

var powersOfTwo = [2,4,8,16,32,...];

The reasoning behind this is that I would be limiting the number of teams that could be put in the system (however, I don't think a person would have over 256 teams).

I am creating a simple bracket system and I need a way to check if there are a correct number of teams, OR if my program needs to pensate for bye rounds.

Right now, I am checking for "powers of two" with this function:

function validBracket(data) {
    var x = data.teams.length;
    return ((x != 0) && !(x & (x - 1)));
}

This works pretty well, but I am needing to know how many Bye rounds to add. For instance, if I had 16 teams, I would not need to add anymore teams. However, if I had 12 teams, I would need the first 4 teams to get a bye round.

How can I calculate number of bye rounds to add to my bracket? And would hard-coding an array of powers of two be better?

In pseudo code, something like this is what i was thinking of:

if(validateBracket(data)) {
    // Valid number of teams (power of two). Keep going.
} else {
    var byeRounds = calculateByeRounds();
}

NOTE: I would rather not use an array of powers of two like below:

var powersOfTwo = [2,4,8,16,32,...];

The reasoning behind this is that I would be limiting the number of teams that could be put in the system (however, I don't think a person would have over 256 teams).

Share Improve this question asked Jun 14, 2015 at 2:37 Hunter MitchellHunter Mitchell 7,30318 gold badges75 silver badges119 bronze badges 2
  • 2 Calculate the next power of 2 here and subtract from your current integer – Drakes Commented Jun 14, 2015 at 2:42
  • Hint: Each match eliminates one player. So a tournament of 12 players requires 11 matches. Tournaments are easy to design for powers of two, so make 12 players up to 16 by introducing 4 fictitious opponents. These are the byes. – Colonel Panic Commented Jun 16, 2015 at 10:57
Add a ment  | 

2 Answers 2

Reset to default 11
var needed = (1 << Math.ceil(Math.log2(n))) - n;

More generalized solution for extreme cases:

var needed = Math.pow(2, Math.ceil(Math.log2(n))) - n;

const n = 2;
// Convert n to binary representation as a string and count the number of '1' characters
  
const count = n.toString(2).split('').filter(bit => bit === '1').length;
  
// Return true if there is exactly one '1' bit in the binary representation of n
 
const result = count === 1 ? `${n} is power of two` : `${n} is NOT power of two`;
 
console.log(result);

发布评论

评论列表(0)

  1. 暂无评论