leading_ones
leading_ones(x::Integer) > Integer
Number of ones leading the binary representation of x
.
julia> leading_ones(UInt32(2 ^ 32  2))
31
Examples
The leading_ones
function in Julia returns the number of ones leading the binary representation of an Integer
value.
julia> leading_ones(UInt32(2 ^ 32  2))
31
Here are some examples of how to use the leading_ones
function:

Count leading ones in a binary number:
julia> leading_ones(UInt8(0b11110000)) 4
This example counts the number of leading ones in the binary representation of the
UInt8
value0b11110000
. 
Determine the number of leading ones in a larger integer:
julia> leading_ones(UInt64(0b11111111111111110000000000000000)) 16
Here,
leading_ones
counts the number of ones at the beginning of the binary representation of theUInt64
value0b11111111111111110000000000000000
.  Count leading ones in a negative number:
julia> leading_ones(Int16(0b1111000000000000)) 4
In this example,
leading_ones
counts the number of leading ones in the binary representation of the negativeInt16
value0b1111000000000000
.
Please note that leading_ones
works with any Integer
type and returns an Integer
value representing the count of leading ones.
If the input x
is zero, the function will return 0
since there are no leading ones in the binary representation.
julia> leading_ones(UInt8(0))
0
No common mistakes are associated with using the leading_ones
function.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.