binary

package
v0.0.3-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2021 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package binary describes algorithms that use binary operations for different calculations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPowerOfTwo

func IsPowerOfTwo(x int) bool

IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function:

  10...0
& 01...1
  00...0 -> 0

This is also true for 0, which is not a power of 2, for which we have to add and extra condition.

func IsPowerOfTwoLeftShift

func IsPowerOfTwoLeftShift(number uint) bool

IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2

func MeanUsingAndXor

func MeanUsingAndXor(a int, b int) int

func MeanUsingRightShift

func MeanUsingRightShift(a int, b int) int

func ReverseBits

func ReverseBits(number uint) uint

ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result and finally move on to the next bit in the input number. Repeat this till all its bits are processed.

func XorSearchMissingNumber

func XorSearchMissingNumber(a []int) int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL