How to count the trailing zeros in a binary number using TypeScript

1 Answer

0 votes
function countTrailingZeros(n: number): number {
    let binaryStr: string = n.toString(2); // Convert to binary string
    let trimmedStr: string = binaryStr.replace(/0+$/, ''); // Remove trailing zeros

    return binaryStr.length - trimmedStr.length;
}

let n: number = 80; // binary: 1010000

console.log("Number of Trailing Zeros:", countTrailingZeros(n));



 
/*
run:

"Number of Trailing Zeros:",  4 
     
*/

 



answered Jul 23, 2025 by avibootz
...