Round Up the Next and Previous Power of 2 in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:31:42

243 Views

ExamplesFor example, n = 12 => Previous number power of 2 is 8.For example, n = 20 => Previous number power of 2 is 16.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Perform n | (n >> k), where k is 1, 2, 4, 8, 16.Step 3 − At the end, return n - (n >> 1).Example Live Demopackage main import "fmt" func PreviousPowOf2(n int) int{    n = n | (n >> 1)    n = n | (n >> 2)    n = n | (n >> 4)    n ... Read More

Round Up to the Next Highest Power of 2 in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 05:29:55

571 Views

ExamplesFor example, n = 12 => Next number power of 2 is 16.For example, n = 20 => Next number power of 2 is 32.Approach to solve this problemStep 1 − Define method, that accepts a number n.Step 2 − Iterate k := 1 until k < n.Step 3 − In a loop, calculate k

Find Minimum and Maximum Number Using Binary Operations in Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:39:20

233 Views

ExamplesFor example, x = 12, y = 15 => Maximum number is 15.For example, x = 13, y = 17 => Minimum number is 13.Approach to solve this problemStep 1 − Define method, findMax and findMin, that accept two integer x and y.Step 2 − Return integer according to defined method.Example Live Demopackage main import "fmt" func FindMax(x, y int){    fmt.Printf("Maximum element in %d, and %d is: %d", x, y, x - ((x - y) &    ((x - y) >> 31))) } func FindMin(x, y int) {    fmt.Printf("Minimum element in %d, and %d is: %d", x, y, y ... Read More

Count Set Bits in an Integer Using Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:38:50

2K+ Views

ExamplesFor example, 101, 11, 11011 and 1001001 set bits count 2, 2, 4 and 3 respectively.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Count the number of 1s; return count.Example Live Demopackage main import (    "fmt"    "strconv" ) func NumOfSetBits(n int) int{    count := 0    for n !=0{       count += n &1       n >>= 1    }    return count } func main(){    n := 20    fmt.Printf("Binary representation of %d is: %s.", n,    strconv.FormatInt(int64(n), 2))    fmt.Printf("The total number of set bits in %d is %d.", n, NumOfSetBits(n)) }OutputBinary representation of 20 is: 10100. The total number of set bits in 20 is 2.

Check If Binary Representation of a Number Is Palindrome in Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:26:31

262 Views

ExamplesFor example, 101, 11, 11011, 1001001 are Palindrome. 100, 10010 are not Palindrome.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Traverse the converted binary representation from both side and check whether representation is palindrome or not.Example Live Demopackage main import (    "fmt"    "strconv" ) func IsPalindrome(n int) bool{    rev := 0    k := n    for k != 0 {       rev = (rev > 1    }    return n == rev } func main(){    n := 3    fmt.Printf("Binary representation of %d is: %s.", n,    strconv.FormatInt(int64(n), 2))    if IsPalindrome(n) == true{       fmt.Println("Palindrome")    } else {       fmt.Println("Not a Palindrome")    } }OutputBinary representation of 3 is: 11. Palindrome

Count Flips to Convert One Integer to Another in Golang

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:20:53

152 Views

ExamplesConsider two numbers m = 65 => 01000001 and n = 80 => 01010000Number of bits flipped is 2.Approach to solve this problemStep 1 − Convert both numbers into bits.Step 2 − Count number of bits are flipped.Example Live Demopackage main import (    "fmt"    "strconv" ) func FindBits(x, y int) int{    n := x ^ y    count := 0    for ;n!=0; count++{       n = n & (n-1)    }    return count } func main(){    x := 65    y := 80    fmt.Printf("Binary of %d is: %s.", x, strconv.FormatInt(int64(x), 2))   ... Read More

Find the Parity of a Given Number in Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:17:40

910 Views

Definition − Parity refers to the count of 1s. If count of 1s is even, then it’s even parity; and if the count of 1s is odd, then the parity is Odd.ExamplesConsider n = 20(00010100)Parity of the given number 20 is even.Approach to solve this problemStep 1 − Define a method, where n and is an argument, return type is int.Step 2 − Calculate the count of 1s in the given number’s bit.Example Live Demopackage main import (    "fmt"    "strconv" ) func FindParity(n int) bool {    parity := false    for n != 0 {       ... Read More

Find Position of the Rightmost Set Bit in Golang

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:11:57

319 Views

ExamplesConsider n = 20(00010100)Now return log2(20 & -20) => 2+1 => 3Approach to solve this problemStep 1 − Define a method, where n and is an argument, return type is int.Step 2 − Return log2(n & -n)+1.Examplepackage main import (    "fmt"    "math"    "strconv" ) func FindRightMostSetBit(n int) int {    if (n & 1) != 0{       return 1    }    return int(math.Log2(float64(n & -n))) + 1 } func main(){    var n = 20    fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2))    fmt.Printf("Position of the rightmost set bit of the given number %d is %d.", n, FindRightMostSetBit(n)) }OutputBinary of 20 is: 10100. Position of the rightmost set bit of the given number 20 is 3.

Check If Number Is Power of 2 in Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:06:52

667 Views

ExamplesConsider n = 16(00010000)Now find x = n-1 => 15(00001111) => x & n => 0Approach to solve this problemStep 1 − Define a method, where n and is an argument, returns type is int.Step 2 − Perform x = n & n-1.Step 3 − If x is 0, then the given number is power of 2; else not.Example Live Demopackage main import (    "fmt"    "strconv" ) func CheckNumberPowerOfTwo(n int) int {    return n & (n-1) } func main(){    var n = 16    fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2))    flag := CheckNumberPowerOfTwo(n)   ... Read More

Toggle the Kth Bit of a Given Number in Go

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 11:01:23

156 Views

ExamplesConsider n = 20(00010100), k = 3.After toggling the kth bit of the given number: 00010000 => 16.Approach to solve this problemStep 1 − Define a method, where n and k would be the arguments, returns type is int.Step 2 − Perform AND operation with n ^ (1

Advertisements