Bits Package in Golang

Bits package in Golang has functions for changing a binary number's individual bits. This package is very helpful in low-level applications like networking, cryptography, and bitwise operations. This post will go through the Golang bits package and show you how to utilise it in a real-world application.

Introduction to the Bits Package

The bits package offers tools for working with a binary number's constituent bits. The "BitArray" type, which represents a series of bits, is the main type in the bits package. An implementation of the "BitArray" type is a slice of "uint64" values, where each "uint64" value has 64 bits.

  • NewBitArray(n int) *BitArray − creates a new BitArray of length n.

  • SetBit(n int) − sets the bit at position n to 1.

  • ClearBit(n int) − clears the bit at position n.

  • FlipBit(n int) − flips the bit at position n.

  • GetBit(n int) int − returns the value of the bit at position n (0 or 1).

  • String() string − returns a string representation of the BitArray.

Using the Bits Package in a Practical Scenario

Let's use a real-world example to show how to use the bits package. Consider developing a function that takes an IPv4 address in the form of a string and transforms it into a 32-bit integer. To accomplish this, we may make use of the SetBit() function and the BitArray type −


package main

import (

func ipv4ToUint32(ipv4 string) uint32 {
   parts := strings.Split(ipv4, ".")
   if len(parts) != 4 {
      panic("invalid IPv4 address")

   var ba bitarray.BitArray
   ba = bitarray.NewBitArray(32)

   for i := 0; i < 4; i++ {
      part, err := strconv.Atoi(parts[i])
      if err != nil {
         panic("invalid IPv4 address")
      if part < 0 || part > 255 {
         panic("invalid IPv4 address")

      for j := 0; j < 8; j++ {
         if (part >> uint(7-j)) & 0x01 == 1 {

   return uint32(ba.GetUint64(0))

func main() {
   ipv4 := ""
   ipv4Int := ipv4ToUint32(ipv4)
   fmt.Printf("%s -> %d\n", ipv4, ipv4Int)

In this example, we first divide the IPv4 address into its four components (separated by dots), and then we confirm the validity of the address. Then, we build a brand-new BitArray with a length of 32 (4 bytes), and we iterate through each component of the address. We change each component into an integer and then loop through its 8 bits. Using the SetBit() function, we set the corresponding bit in the BitArray if the bit is already set. The BitArray is then changed into an uint32 value and returned.

When we execute the programme, the following output ought to appear −

Output -> 3232235521


Go's bits package offers practical tools for handling bit-level operations. It is a potent and effective approach to work with memory's bits and bytes, giving developers the ability to work with data at a basic level. The package's features are well-documented and easy to use. Developers can execute bitwise operations, set and clear bits, and extract bitfields from data using the bits package. In general, the bits package is a crucial tool for any Go developer working with binary data and low-level data structures.

Updated on: 06-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started