Definition of authority 2?

I am creating a simple bracket system, and I need a way to check if there is the right number of commands OR if my program needs to compensate for byte rounds.

Now I am checking the "powers of two" with this function:

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

This works very well, but I need to know how many buy-ins to add. For example, if I had 16 teams , I no longer needed to add teams. However, if I had 12 teams , I would need the first 4 teams to get a byte round.

How can I calculate the number of rounds of a round to add to my bracket? And will hard coding an array of two degrees be better?

In the pseudo code, something like this that I was thinking about:

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

NOTE: I would prefer not to use an array of capacities, as shown below:

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

The reason for this is that I would limit the number of teams that could be added to the system (however, I do not think that a person would have more than 256 teams).

+6
source share
1 answer
 var needed = (1 << Math.ceil(Math.log2(n))) - n; 

A more generalized solution for extreme cases:

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

Source: https://habr.com/ru/post/989075/


All Articles