How do you find the position of the most significant bit (MSB) of an unsigned integer in JavaScript

2 Answers

0 votes
'use strict';

/**
 * Returns the zero-based position of the most significant set bit (MSB)
 * in an unsigned 32-bit integer.
 *
 * Examples:
 *   1  -> 0
 *   2  -> 1
 *   8  -> 3
 *   15 -> 3
 *
 * @param {number} value A non-negative integer in the range 0..0xFFFFFFFF.
 * @returns {number}
 *   The zero-based position of the highest set bit,
 *   or -1 if the value is 0.
 * @throws {RangeError}
 *   If the value is not a valid unsigned 32-bit integer.
 */
function getMsbPosition(value) {
    if (!Number.isInteger(value) ||
        value < 0 ||
        value > 0xFFFFFFFF) {
        throw new RangeError(
            'Value must be an unsigned 32-bit integer.'
        );
    }

    if (value === 0) {
        return -1;
    }

    // clz32() returns the number of leading zero bits
    // in the 32-bit unsigned representation.
    return 31 - Math.clz32(value);
}

/**
 * Program entry point.
 */
function main() {
    const testValues = [
        0,
        1,
        2,
        8,
        15,
        0x1234,
        0x80000000
    ];

    for (const value of testValues) {
        const position = getMsbPosition(value);

        if (position >= 0) {
            console.log(
                `Value: ${value}, MSB position: ${position}`
            );
        } else {
            console.log(
                `Value: ${value}, no bits are set`
            );
        }
    }
}

main();



/*
run:

Value: 0, no bits are set
Value: 1, MSB position: 0
Value: 2, MSB position: 1
Value: 8, MSB position: 3
Value: 15, MSB position: 3
Value: 4660, MSB position: 12
Value: 2147483648, MSB position: 31

*/

 



answered Oct 1 by avibootz
0 votes
'use strict';

/**
 * Returns the zero-based position of the most significant set bit (MSB)
 * in an unsigned 32-bit integer.
 *
 * Examples:
 *   1  -> 0
 *   2  -> 1
 *   8  -> 3
 *   15 -> 3
 *
 * @param {number} value A non-negative integer in the range 0..0xFFFFFFFF.
 * @returns {number}
 *   The zero-based position of the highest set bit,
 *   or -1 if the value is 0.
 * @throws {RangeError}
 *   If the value is not a valid unsigned 32-bit integer.
 */
function getMsbPosition(value) {
    if (!Number.isInteger(value) || value < 0) {
        throw new RangeError(
            'Value must be a non-negative integer.'
        );
    }

    if (value === 0) {
        return -1;
    }

    let position = 0;

    while (value > 1) {
        value >>>= 1;
        position++;
    }

    return position;
}

/**
 * Program entry point.
 */
function main() {
    const testValues = [
        0,
        1,
        2,
        8,
        15,
        0x1234,
        0x80000000
    ];

    for (const value of testValues) {
        const position = getMsbPosition(value);

        if (position >= 0) {
            console.log(
                `Value: ${value}, MSB position: ${position}`
            );
        } else {
            console.log(
                `Value: ${value}, no bits are set`
            );
        }
    }
}

main();


/*
run:

Value: 0, no bits are set
Value: 1, MSB position: 0
Value: 2, MSB position: 1
Value: 8, MSB position: 3
Value: 15, MSB position: 3
Value: 4660, MSB position: 12
Value: 2147483648, MSB position: 31

*/

 



answered Oct 1 by avibootz
...