Server Side Programming Articles - Page 1152 of 2650

Golang Program to find the parity of a given number.

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

925 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

Golang Program to find the position of the rightmost set bit

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

330 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.

Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop

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

680 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

Golang Program to toggle the Kth of the given number n.

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

163 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

Golang program to check if k’th bit is set for a given number or not.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:58:33

2K+ Views

ExamplesConsider n = 20(00010100), k = 3So, the result after turning off the 3rd bit => 00010000 & (1

Golang Program to turn on the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:56:25

193 Views

ExampleFor example consider n = 20(00010100), k = 4. So result after turning on 4th bit => 00010000 | (1

Golang program to turn off the k’th bit in a number.

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:54:15

172 Views

ExampleConsider n = 20(00010100), k = 3 The result after turning off the 3rd bit => 00010000 & ^(1 16sApproach to solve this problemStep 1 − Define a method, where n and k would be the arguments, return type is int.Step 2 − Perform AND operation with n & ^(1

Golang program to define a binary tree

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 10:52:12

651 Views

Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Example Live Demopackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if ... Read More

Rotating an image using Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:45:51

836 Views

In this program, we will rotate an image using the pillow library. The rotate() function in the Image class takes in angle of rotation.Original ImageAlgorithmStep1: Import Image class from Pillow. Step 2: Open the image. Step 3: Rotate the image. Step 4: Display the output.Example Codefrom PIL import Image im = Image.open('testimage.jpg') im.rotate(45).show()Output

Cropping an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:46:10

290 Views

In this program, we will crop an image using the Pillow library. We will use the crop() function for the same. The function takes left, top, right, bottom pixel coordinates to crop the image.Original ImageAlgorithmStep 1: Import Image from Pillow. Step 2: Read the image. Step 3: Crop the image using the crop function. Step 4: Display the output.Example Codefrom PIL import Image im = Image.open('testimage.jpg') width, height = im.size left = 5 top = height / 2 right = 164 bottom = 3 * height / 2 im1 = im.crop((left, top, right, bottom)) im1.show()Output

Advertisements