Bit-Manipulation

AlgorithmsCollection.count_total_bitsFunction
count_total_bits(n::Int64)

Calculates the total number of bits from 1 to n for a given integer n by combing a while-loop for building the binary subsets with a range for-loop for counting the bits.

Arguments

  • n::Int64: Integer value which has to be evaluated.

Examples

julia> import ClassicAlgorithmsCollections
julia> ClassicAlgorithmsCollections.count_total_bits(17)
35
AlgorithmsCollection.get_single_integerFunction
get_single_integer(array::Array{Int64,1})

For finding the single element in an array, where every other elements appear three times.

Arguments

  • array::Array{Int64,1}: Unsorted array with threetimes elements and one single element.

Examples

julia> import ClassicAlgorithmsCollections
julia> arr = [12, 1, 12, 3, 12, 1, 1, 2, 3, 3]
julia> ClassicAlgorithmsCollections.get_single_integer(arr)
2
AlgorithmsCollection.int2binaryFunction
int2binary(n::Int64, array=zeros(Int64,0))

Converts any integer to a binary representation as initiger array with 0, 1.

Arguments

  • n::Int64: Integer value which has to be converted
  • array=zeros(Int64,0): Empty array for return

Examples

julia> import ClassicAlgorithmsCollections
julia> ClassicAlgorithmsCollections.int2binary(131)
([1,0,0,0,0,0,1,1],1)
AlgorithmsCollection.magic_numberFunction
magic_number(n::Int64))

A magic number has to build as the power of 5 or the sum of unique powers of 5. First few magic numbers are: 1. 5 2. 5^2 = 25 3. 5 + 25 = 30 4. 5^3 = 125 5. 125 + 5 = 130

Arguments

  • n::Int64: Integer value of the number, which has to become a magic number

Examples

julia> import ClassicAlgorithmsCollections
julia> ClassicAlgorithmsCollections.magic_number(5)
130
AlgorithmsCollection.maximum_xor_subarrayFunction
maximum_xor_subarray(array::Array{Int64,1})

For finding the maximum XOR value of a subarray by running two nested for-loops. The first for-loop is intializing the current XOR value, the second for-loop is generating the BIT-XOR and compared the current value with the temporary largest value.

Arguments

  • array::Array{Int64,1}: Unsorted array

Examples

julia> import ClassicAlgorithmsCollections
julia> arr = [8, 1, 2, 12, 45, 100, 43]
julia> ClassicAlgorithmsCollections.maximum_xor_subarray(arr)
110
AlgorithmsCollection.sum_of_bit_differenceFunction
sum_of_bit_difference(array::Array{Int64,1})

For finding the sum of bit differences in all pairs of an array, the array will be transformed into a series of subarrays for a given bit-length.

Arguments

  • array::Array{Int64,1}: Unsorted array

Examples

julia> import ClassicAlgorithmsCollections
julia> arr = [8, 1, 2, 12, 45, 100, 43]
julia> ClassicAlgorithmsCollections.maximum_xor_subarray(arr)
104
AlgorithmsCollection.swapping_even_odd_bitsFunction
swapping_even_odd_bits(n::Int64))

For a given integer value, all odd bits will be swapped with even bits.

Arguments

  • n::Int64: Integer value of the number, which has to be swapped.

Examples

julia> import ClassicAlgorithmsCollections
julia> ClassicAlgorithmsCollections.function swapping_even_odd_bits(23)
43

Notes


The definition of the even and odd bits:

even_bits = n & 0xAAAAAAAA
odd_bits = n & 0x55555555