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:

 # Julia program to illustrate # the use of count_ones() method  # Getting number of ones present # in the binary representation # of the specified number.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:

 # Julia program to illustrate # the use of count_zeros() method  # Getting number of zeros present # in the binary representation # of the specified number.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
My Personal Notes arrow_drop_up