bits.Div() function in Golang with Examples

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 Div() function which is used to find the quotient and remainder of (a, b) divided by c, i.e., q = (a, b)/c, r = (a, b)%c with the dividend bits’ upper half in parameter a and the lower half in parameter b. This function panics if c == 0 (division by zero) or c <= a (quotient overflow). To access Div() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func Div(a, b, c uint) (q, r uint)

Parameters: This function takes three parameters of uint type, i.e., a, b, and c.

Return Value: This function return two values of uint type, i.e., q and r. Here q is known as quotient and r is known as the remainder.

Example 1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate bits.Div() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Finding quotient and remainder
    // Using Div() function
    q, r := bits.Div(1, 12, 2)
    fmt.Println("Quotient:", q)
    fmt.Println("Remainder:", r)
  
}

chevron_right


Output:

Quotient: 9223372036854775814
Remainder: 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate bits.Div() Function
  
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Finding quotient and remainder
    // Using Div() function
    var a, b, c uint = 1, 13, 3
    q, r := bits.Div(a, b, c)
    fmt.Println("Number 1:", a)
    fmt.Println("Number 2:", b)
    fmt.Println("Number 3:", c)
    fmt.Println("Quotient:", q)
    fmt.Println("Remainder:", r)
  
}

chevron_right


Output:

Number 1: 1
Number 2: 13
Number 3: 3
Quotient: 6148914691236517209
Remainder: 2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.