The bits.Sub() Function in Golang is used to find the difference of a, b and borrow, i.e. diff = a – b – borrow. Here the borrow must be 0 or 1; otherwise, the behavior is undefined. To access this function, one needs to imports the math/bits package in the program. The return value of the borrowOutput will be always 0 or 1 in any case.
Syntax:
func Sub(a, b, borrow uint) (diff, borrowOut uint)
Parameters: This function takes three parameters of uint type, i.e., a, b, and borrow. The value of borrow parameter is either 1 or 0
Return Value: This function return two values of uint type, i.e., diff and borrowOut. Here diff contains the result of a – b – borrow and borrowOut is either 1 or 0.
Example 1:
// Golang program to illustrate bits.Sub() Function package main import ( "fmt" "math/bits") // Main function func main() { // Finding diff and borrowOu // of the specified numbers // Using Sub() function nvalue_1, borrowOut := bits.Sub(4, 3, 0) fmt.Println("Diff:", nvalue_1) fmt.Println("BorrowOut :", borrowOut ) } |
Output:
Diff: 1 BorrowOut : 0
Example 2:
// Golang program to illustrate bits.Sub() Function package main import ( "fmt" "math/bits") // Main function func main() { // Finding diff and borrowOut // of the specified numbers // Using Sub() function var a, b, borrow uint = 10, 5, 1 Diff, borrowOut := bits.Sub(a, b, borrow) fmt.Println("Number 1:", a) fmt.Println("Number 2:", b) fmt.Println("Borrow :", borrow) fmt.Println("Diff:", Diff) fmt.Println("BorrowOut :", borrowOut) } |
Output:
Number 1: 10 Number 2: 5 Borrow : 1 Diff: 4 BorrowOut : 0
Recommended Posts:
- reflect.New() Function in Golang with Examples
- time.After() Function in Golang With Examples
- reflect.Tag.Get() Function in Golang with Examples
- reflect.Zero() Function in Golang with Examples
- math.Jn() Function in Golang With Examples
- filepath.Ext() Function in Golang With Examples
- filepath.Dir() Function in Golang With Examples
- strings.Map() Function in Golang With Examples
- fmt.Scanln() Function in Golang With Examples
- complx.Inf() Function in Golang With Examples
- math.J1() Function in Golang With Examples
- math.J0() Function in Golang With Examples
- math.Inf() Function in Golang With Examples
- io.MultiWriter() Function in Golang with Examples
- time.Until() Function in Golang With Examples
- fmt.Sscanln() Function in Golang With Examples
- io.ReadFull() Function in Golang with Examples
- fmt.Scanf() Function in Golang With Examples
- fmt.Scan() Function in Golang With Examples
- io.ReadAtLeast() Function in Golang with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

