The bits.Sub32() 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.
func Sub32(a, b, borrow uint32) (diff, borrowOut uint32)
Parameters: This function takes three parameters of uint32 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 uint32 type, i.e., diff and borrowOut. Here diff contains the result of a – b – borrow and borrowOut is either 1 or 0.
Diff: 4 BorrowOut : 0
Example 2: Here, you can see that the result is not as excepted as we have taken the value of borrow as 3. So if we are taking borrow input other than 1 and 0 then the behavior will be undefined.
Number 1: 4 Number 2: 15 Borrow : 3 Diff: 4294967282 BorrowOut : 1
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.