Skip to content
Related Articles

Related Articles

Counting trailing ones and zeros in binary representation of a number in Julia – trailing_ones() and trailing_zeros() Methods
  • Last Updated : 26 Mar, 2020

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

Syntax: trailing_ones(x::Integer)

Parameters:

  • x::Integer: Specified integer number.

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

Example:






# Julia program to illustrate 
# the use of trailing_ones() method
  
# Getting the number of ones trailing
# the binary representation of 
# the specified number.
println(trailing_ones(2))
println(trailing_ones(15))
println(trailing_ones(13))
println(trailing_ones(3))

Output:

0
4
1
2

trailing_zeros()

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

Syntax: trailing_zeros(x::Integer)

Parameters:

  • x::Integer: Specified integer number.

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

Example:




# Julia program to illustrate 
# the use of trailing_zeros() method
  
# Getting the number of zeros trailing
# the binary representation of 
# the specified number.
println(trailing_zeros(2))
println(trailing_zeros(56))
println(trailing_zeros(100))
println(trailing_zeros(32))

Output:

1
3
2
5

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 :