Skip to content
Related Articles

Related Articles

bits.Add32() Function in Golang with Examples
  • Last Updated : 15 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Add32() function which is used to find the sum with the carry of a, b and carry, i.e, sum = a + b + carry. Here the value of carry must be 0 or 1, otherwise, the behavior is undefined. To access the Add32() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func Add32(a, b, carry uint32) (sum, carryout uint32)

Parameters: This function takes three parameters of uint32 type, i.e., a, b, and carry. The value of carry parameter is either 1 or 0.

Return Value: This function return two values of uint32 type, i.e., sum and carryout. Here sum contains the result of a + b + carry and carryout is either 1 or 0.

Example 1:






// Golang program to illustrate bits.Add32() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Finding sum and carry 
    // of the specified numbers
    // Using Add32() function
    nvalue_1, carry := bits.Add32(20, 12, 1)
    fmt.Println("Sum:", nvalue_1)
    fmt.Println("Carry:", carry)
  
}

Output:

Sum: 33
Carry: 0

Example 2:




// Golang program to illustrate bits.Add32() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Finding sum and carry 
    // of the specified numbers
    // Using Add32() function
    var a, b, carry uint32 = 31, 34, 0
    sum, carryout := bits.Add32(a, b, carry)
    fmt.Println("Number 1:", a)
    fmt.Println("Number 2:", b)
    fmt.Println("Carry:", carry)
    fmt.Println("Sum:", sum)
    fmt.Println("Carry:", carryout)
  
}

Output:

Number 1: 31
Number 2: 34
Carry: 0
Sum: 65
Carry: 0

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :