Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bit array
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == Given a big file of [[IPv4]] addresses (more than 100 GB) β we need to count unique addresses. If we use generic <code>map[string]bool</code> β we will need more than 64 GB of [[Random-access memory|RAM]], so lets use the '''bit map''', in [[Go (programming language)|Go]]:<syntaxhighlight lang="go" line="1"> package main import ( "bufio" "fmt" "math/bits" "os" ) // bitsetSize is the number of bytes needed for 2^32 bits (512 MiB) const bitsetSize = 1 << 29 func main() { file, err := os.Open("ip_addresses") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() bitset := [bitsetSize]byte{} // Use a buffered scanner with a larger buffer scanner := bufio.NewScanner(file) const maxBuffer = 64 * 1024 // 64 KB buffer buf := make([]byte, 0, maxBuffer) scanner.Buffer(buf, maxBuffer) // Process each line for scanner.Scan() { line := scanner.Bytes() // Parse the IP address manually from bytes ip := parseIPv4(line) // Set the bit byteIndex := ip >> 3 // Divide by 8 bitIndex := ip & 7 // Bit position 0-7 bitset[byteIndex] |= 1 << bitIndex } // Check for scanning errors if err := scanner.Err(); err != nil { fmt.Println("Error reading file:", err) return } var count uint64 for i := 0; i < bitsetSize; i++ { count += uint64(bits.OnesCount8(bitset[i])) } fmt.Println("Number of unique IPv4 addresses:", count) } func parseIPv4(line []byte) (ip uint32) { i := 0 // Octet 1 n := uint32(line[i] - '0') for i = 1; line[i] != '.'; i++ { n = n*10 + uint32(line[i]-'0') } ip |= n << 24 i++ // Skip the dot // Octet 2 n = uint32(line[i] - '0') i++ for ; line[i] != '.'; i++ { n = n*10 + uint32(line[i]-'0') } ip |= n << 16 i++ // Skip the dot // Octet 3 n = uint32(line[i] - '0') i++ for ; line[i] != '.'; i++ { n = n*10 + uint32(line[i]-'0') } ip |= n << 8 i++ // Skip the dot // Octet 4 n = uint32(line[i] - '0') i++ for ; i < len(line); i++ { n = n*10 + uint32(line[i]-'0') } ip |= n return ip } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)