Counting ones and zeros in binary representation of a number in Julia – count_ones() and count_zeros() Methods
Last Updated :
26 Mar, 2020
The count_ones()
is an inbuilt function in julia which is used to calculate the number of ones present in the binary representation of the specified number.
Syntax: count_ones(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the calculated number of ones present in the binary representation of the specified number.
Example:
println(count_ones( 0 ))
println(count_ones( 1 ))
println(count_ones( 15 ))
println(count_ones( 20 ))
|
Output:
0
1
4
2
count_zeros
The count_zeros()
is an inbuilt function in julia which is used to calculate the number of zeros present in the binary representation of the specified number.
Syntax: count_zeros(x::Integer)
Parameters:
- x::Integer: Specified integer number.
Returns: It returns the calculated number of zeros present in the binary representation of the specified number.
Example:
println(count_zeros( 0 ))
println(count_zeros( 1 ))
println(count_zeros(Int32( 2 ^ 16 - 1 )))
println(count_zeros(Int32( 2 ^ 16 - 10 )))
|
Output:
64
63
16
18
Share your thoughts in the comments
Please Login to comment...