Skip to content
Related Articles

Related Articles

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.

Syntax: leading_ones(x::Integer)

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

leading_zeros()

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.

Syntax: leading_zeros(x::Integer)

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :