Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
bits Package in Golang
Next article icon

bits Package in Golang

Last Updated : 08 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Go language provides inbuilt support for bit counting and manipulation functions for the predeclared unsigned integer types with the help of the bits package.
Function Description
Add This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add32 This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add64 This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Div This function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (hi, lo)%x with the dividend bits' upper half in parameter h and the lower half in parameter l.
Div32 This function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (h, l)%x with the dividend bits' upper half in parameter h and the lower half in parameter l.
Div64 This function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (h, l)%x with the dividend bits' upper half in parameter h and the lower half in parameter l.
LeadingZeros This function returns the number of leading zero bits in y. The result is UintSize for x == 0.
LeadingZeros16 This function returns the number of leading zero bits in y. The result is 16 for y == 0.
LeadingZeros32 This function returns the number of leading zero bits in y. The result is 32 for y == 0.
LeadingZeros64 This function returns the number of leading zero bits in y. The result is 64 for y == 0.
LeadingZeros8 This function returns the number of leading zero bits in y. The result is 8 for y == 0.
Len This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len16 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len32 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len64 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len8 This function returns the minimum number of bits required to represent y. the result is 0 for y == 0.
Mul This function is used to return the full-width product of a and b, i.e., (hi, lo) = a * b with the product bits' upper half returned in hi and the lower half returned in lo.
Mul32 This function is used to return the 64-bit product of a and b, i.e., (hi, lo) = a * b with the product bits' upper half returned in hi and the lower half returned in lo.
Mul64 This function is used to return the 128-bit product of a and b, i.e., (hi, lo) = a * b with the product bits' upper half returned in hi and the lower half returned in lo.
OnesCount This function returns the number of one bits ("population count") in y.
OnesCount16 This function returns the number of one bits ("population count") in y.
OnesCount32 This function returns the number of one bits ("population count") in y.
OnesCount64 This function returns the number of one bits ("population count") in y.
OnesCount8 This function returns the number of one bits ("population count") in y.
Rem This function returns the remainder of (hi, lo) divided by x.
Rem32 This function returns the remainder of (hi, lo) divided by x.
Rem64 This function returns the remainder of (hi, lo) divided by x.
Reverse This function returns the value of y with its bits in reversed order.
Reverse16 This function returns the value of y with its bits in reversed order.
Reverse32 This function returns the value of y with its bits in reversed order.
Reverse64 This function returns the value of y with its bits in reversed order.
Reverse8 This function returns the value of y with its bits in reversed order.
ReverseBytes This function returns the value of x with its bytes in reversed order.
ReverseBytes16 This function returns the value of x with its bytes in reversed order.
ReverseBytes32 This function returns the value of x with its bytes in reversed order.
ReverseBytes64 This function returns the value of x with its bytes in reversed order.
RotateLeft This function returns the value of y rotated left by (j mod UintSize) bits.
RotateLeft16 This function returns the value of y rotated left by (j mod 16) bits.
RotateLeft32 This function returns the value of y rotated left by (j mod 32) bits.
RotateLeft64 This function returns the value of y rotated left by (j mod 64) bits.
RotateLeft8 This function returns the value of y rotated left by (j mod 8) bits.
Sub This function returns the difference of a, b and borrow: diff = a - b - borrow.
Sub32 This function returns the difference of a, b and borrow: diff = a - b - borrow.
Sub64 This function returns the difference of a, b and borrow: diff = a - b - borrow.
TrailingZeros This function returns the number of trailing zero bits in y. The result is UintSize for y == 0.
TrailingZeros16 This function returns the number of trailing zero bits in y. The result is 16 for y == 0.
TrailingZeros32 This function returns the number of trailing zero bits in y. The result is 32 for y == 0.
TrailingZeros64 This function returns the number of trailing zero bits in y. The result is 64 for y == 0.
TrailingZeros8 This function returns the number of trailing zero bits in y. The result is 8 for y == 0.
Example 1: CSharp
// Golang program to illustrate bits.Sub() Function package main  import (     "fmt"     "math/bits" )  // Main function func main() {      // Finding diff and borrowOu     // of the specified numbers     // Using Sub() function     nvalue_1, borrowOut := bits.Sub(4, 3, 0)     fmt.Println("Diff:", nvalue_1)     fmt.Println("BorrowOut :", borrowOut) } 
Output:
Diff: 1  BorrowOut : 0
Example 2: CSharp
// Golang program to illustrate bits.TrailingZeros64() Function package main  import (     "fmt"     "math/bits" )  // Main function func main() {      // Using TrailingZeros64() function     a := bits.TrailingZeros64(15)     fmt.Printf("Total number of trailing"+             " zero bits in %d: %d", 15, a) } 
Output:
Total number of trailing zero bits in 15: 0

Next Article
bits Package in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang-bits

Similar Reads

    fmt Package in GoLang
    Prerequisite: Packages in GoLang and Import in GoLang  Technically defining, a package is essentially a container of source code for some specific purpose. Packages are very essential as in all programs ranging from the most basic programs to high-level complex codes, these are used. A package ensur
    13 min read
    strconv package in Golang
    Go language provides a strconv package that implements conversions to and from string representations of basic data types. To access the functions of the strconv package you need to import the strconv package in your program with the help of the import keyword. .strconv-golang-table { border-collaps
    5 min read
    Bitwise NOT operator in Golang
    Bitwise NOT operator in the programming world usually takes one number and returns the inverted bits of that number as shown below: Bitwise NOT of 1 = 0 Bitwise NOT of 0 = 1 Example: Input : X = 010101 Output : Bitwise NOT of X = 101010 But Golang doesn't have any specified unary Bitwise NOT(~) or y
    2 min read
    Zero value in Golang
    In Go language, whenever we allocate memory for a variable with the help of declaration or by using new and if the variable is not initialized explicitly, then the value of such types of variables are automatically initialized with their zero value. The initialization of the zero value is done recur
    3 min read
    Interesting Facts About Golang
    Go (also known as Golang or Go language) is the language developed by Google. Go is an open-source, statically-typed compiled, and explicit programming language. According to Google Developers, Go is a dependable and efficient programming language. Go supports Concurrent programming. Go is also a mu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences