bits.Sub64() Function in Golang with Examples
Last Updated :
28 Apr, 2020
The bits.Sub64() 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 Sub64(a, b, borrow uint64) (diff, borrowOut uint64)
Parameters: This function takes three parameters of uint64 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 uint64 type, i.e., diff and borrowOut. Here diff contains the result of a – b – borrow and borrowOut is either 1 or 0.
Example 1:
package main
import (
"fmt"
"math/bits"
)
func main() {
nvalue_1, borrowOut := bits.Sub64(11, 5, 0)
fmt.Println( "Diff:" , nvalue_1)
fmt.Println( "BorrowOut :" , borrowOut)
}
|
Output:
Diff: 6
BorrowOut : 0
Example 2: Here, you can see that the result is not as excepted as we have taken the value of borrow as 7. So if we are taking borrow input other than 1 and 0 then the behavior will be undefined.
package main
import (
"fmt"
"math/bits"
)
func main() {
var a, b, borrow uint64 = 12, 87, 7
Diff, borrowOut := bits.Sub64(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: 12
Number 2: 87
Borrow : 7
Diff: 18446744073709551540
BorrowOut : 1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...