Golang uint64 type
last modified May 8, 2025
This tutorial explains how to use the uint64
built-in type in Go.
We'll cover uint64 basics with practical examples of working with large numbers.
The uint64 type represents 64-bit unsigned integers in Go. It can store values from 0 to 18,446,744,073,709,551,615. This type is useful for working with very large positive numbers.
In Go, uint64
is part of the numeric types family. It's twice as
large as uint32 and provides a wide range for unsigned integer calculations.
Basic uint64 declaration and initialization
The simplest way to use uint64 is to declare and initialize variables. This
example shows basic uint64 variable usage.
Note: uint64 values must be positive or zero.
package main import "fmt" func main() { var a uint64 = 18446744073709551615 // Maximum uint64 value b := uint64(1000000000000) // Type conversion fmt.Println("a:", a) fmt.Println("b:", b) // Operations sum := a + b fmt.Println("Sum:", sum) product := a * 2 fmt.Println("Product:", product) }
The example shows uint64 declaration, initialization, and basic arithmetic. Note the maximum value and type conversion from integer literal.
Working with large numbers
uint64 is ideal for calculations with very large numbers. This example demonstrates uint64's capacity for big number operations.
package main import "fmt" func main() { // Population calculations worldPopulation := uint64(8045311447) annualGrowth := uint64(80000000) projectedPopulation := worldPopulation + annualGrowth fmt.Println("Projected population:", projectedPopulation) // Distance calculations (in meters) earthToMoon := uint64(384400000) lightYear := uint64(9460730472580800) fmt.Println("Earth to Moon:", earthToMoon) fmt.Println("One light year:", lightYear) }
The example uses uint64 for realistic large number scenarios. It handles population and astronomical distance calculations without overflow.
Bitwise operations with uint64
uint64 is excellent for bit manipulation due to its 64-bit width. This example shows common bitwise operations with uint64.
package main import "fmt" func main() { var flags uint64 = 0 // Set flags flags |= 1 << 0 // Set bit 0 flags |= 1 << 63 // Set bit 63 fmt.Printf("Flags: %064b\n", flags) // Check flag if flags & (1 << 63) != 0 { fmt.Println("Bit 63 is set") } // Clear flag flags &^= 1 << 0 fmt.Printf("Flags after clear: %064b\n", flags) }
The example demonstrates setting, checking, and clearing bits in a uint64. Bitwise operations are fundamental for low-level programming tasks.
uint64 in binary and hexadecimal
uint64 values can be represented in different bases. This example shows how to work with binary and hexadecimal uint64 literals.
package main import "fmt" func main() { // Binary literal (base 2) bin := uint64(0b11110000111100001111000011110000) fmt.Printf("Binary: %d (0b%064b)\n", bin, bin) // Hexadecimal literal (base 16) hex := uint64(0xFFFFFFFFFFFFFFFF) fmt.Printf("Hexadecimal: %d (0x%016X)\n", hex, hex) // Convert between bases fromHex := uint64(0xDEADBEEF) fmt.Printf("0xDEADBEEF in decimal: %d\n", fromHex) fromBin := uint64(0b1010101010101010) fmt.Printf("0b1010101010101010 in decimal: %d\n", fromBin) }
The example shows uint64 literals in binary and hexadecimal formats. It also demonstrates conversion between different numeric bases.
uint64 overflow and underflow
Understanding uint64 boundaries is crucial. This example demonstrates overflow and underflow behavior with uint64.
package main import "fmt" func main() { max := uint64(18446744073709551615) fmt.Println("Max uint64:", max) // Overflow example overflow := max + 1 fmt.Println("Overflow result:", overflow) // Wraps around to 0 // Underflow example var zero uint64 = 0 underflow := zero - 1 fmt.Println("Underflow result:", underflow) // Wraps around to max // Safe operations if max > max - 1 { fmt.Println("Safe comparison works") } }
The example shows what happens when uint64 values exceed their bounds. It demonstrates wrap-around behavior and safe comparison techniques.
Source
This tutorial covered the uint64
type in Go with practical
examples of declaration, operations, and boundary cases.
Author
List all Golang tutorials.