Menu Close

Go – How to get the binary exponent of number as an integer in go

Here, We will learn how to get the binary exponent of number as an integer in go. We can get it by using Ilogb() function in math package in go golang.

Function prototype:

func Ilogb(no float64) int

Return value:

Ilogb() function in math package returns
the binary exponent of number as an integer.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Ilogb(2)
  fmt.Printf("%d\n", no)

  no = math.Ilogb(3)
  fmt.Printf("%d\n", no)

  no = math.Ilogb(1.25)
  fmt.Printf("%d\n", no)

  no = math.Ilogb(-3)
  fmt.Printf("%d\n", no)
}

Output:

1
1
0
1

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *