Counting leading ones and zeros in binary representation of a number in Julia – leading_ones() and leading_zeros() Methods

• Last Updated : 26 Mar, 2020

The `leading_ones()` is an inbuilt function in julia which is used to find the number of ones leading the binary representation of the specified number.

Parameters:

• x::Integer: Specified integer number.

Returns: It returns the number of ones leading the binary representation of the specified number.

Example:

 `# Julia program to illustrate ``# the use of leading_ones() method`` ` `# Getting the number of ones leading``# the binary representation of ``# the specified number.``println(leading_ones(UInt16(``2` `^ ``16` `-` `1``)))``println(leading_ones(UInt16(``2` `^ ``16` `-` `2``)))``println(leading_ones(UInt32(``2` `^ ``32` `-` `1``)))``println(leading_ones(UInt32(``2` `^ ``32` `-` `2``)))`

Output:

```16
15
32
31
```

The `leading_zeros()` is an inbuilt function in julia which is used to find the number of zeros leading the binary representation of the specified number.

Parameters:

• x::Integer: Specified integer number.

Returns: It returns the number of zeros leading the binary representation of the specified number.

Example:

 `# Julia program to illustrate ``# the use of leading_zeros() method`` ` `# Getting the number of zeros leading``# the binary representation of ``# the specified number.``println(leading_zeros(Int16(``5``)))``println(leading_zeros(Int16(``15``)))``println(leading_zeros(Int32(``30``)))``println(leading_zeros(Int32(``36``)))`

Output:

```13
12
27
26
```

My Personal Notes arrow_drop_up