<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Bit_array</id>
	<title>Bit array - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Bit_array"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Bit_array&amp;action=history"/>
	<updated>2026-04-05T08:41:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Bit_array&amp;diff=687162&amp;oldid=prev</id>
		<title>~2025-32447-81: /* Language support */</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Bit_array&amp;diff=687162&amp;oldid=prev"/>
		<updated>2025-11-16T23:11:08Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Language support&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Array data structure that compactly stores bits}}&lt;br /&gt;
{{more citations needed|date=December 2010}}&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;bit array&amp;#039;&amp;#039;&amp;#039; (also known as &amp;#039;&amp;#039;&amp;#039;bit map&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;bit set&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;bit string&amp;#039;&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;&amp;#039;bit vector&amp;#039;&amp;#039;&amp;#039;) is an [[array data structure]] that compactly stores [[bit]]s. It can be used to implement a simple [[set data structure]]. A bit array is effective at exploiting [[bit-level parallelism]] in hardware to perform operations quickly. A typical bit array stores &amp;#039;&amp;#039;kw&amp;#039;&amp;#039; bits, where &amp;#039;&amp;#039;w&amp;#039;&amp;#039; is the number of bits in the unit of storage, such as a [[byte]] or [[Word (computer architecture)|word]], and &amp;#039;&amp;#039;k&amp;#039;&amp;#039; is some nonnegative integer. If &amp;#039;&amp;#039;w&amp;#039;&amp;#039; does not divide the number of bits to be stored, some space is wasted due to [[Fragmentation (computing)|internal fragmentation]].&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
A bit array is a mapping from some domain (almost always a range of integers) to values in the set &amp;lt;math&amp;gt;\{ \texttt{0}, \texttt{1} \}&amp;lt;/math&amp;gt;. The values can be interpreted as dark/light, absent/present, locked/unlocked, valid/invalid, et cetera. The point is that there are only two possible values, so they can be stored in one bit. As with other arrays, the access to a single bit can be managed by applying an index to the array. Assuming its size (or length) to be &amp;#039;&amp;#039;n&amp;#039;&amp;#039; bits, the array can be used to specify a subset of the domain (e.g. &amp;lt;math&amp;gt;\{0, 1, 2, ..., n - 1\}&amp;lt;/math&amp;gt;), where a {{mono|1}}-bit indicates the presence and a {{mono|0}}-bit the absence of a number in the set. This set data structure uses about &amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;w&amp;#039;&amp;#039; words of space, where &amp;#039;&amp;#039;w&amp;#039;&amp;#039; is the number of bits in each [[Word (computer architecture)|machine word]]. Whether the least significant bit (of the word) or the most significant bit indicates the smallest-index number is largely irrelevant, but the former tends to be preferred (on [[Endianness|little-endian]] machines).&lt;br /&gt;
&lt;br /&gt;
A finite [[binary relation]] may be represented by a bit array called a [[logical matrix]]. In the [[calculus of relations]], these arrays are composed with [[matrix multiplication]] where the arithmetic is Boolean, and such a composition represents [[composition of relations]].&amp;lt;ref&amp;gt;[[Irving Copilowish]] (December 1948) &amp;quot;Matrix development of the calculus of relations&amp;quot;, [[Journal of Symbolic Logic]] 13(4): 193–203 [https://www.jstor.org/stable/2267134?seq=1#page_scan_tab_contents Jstor link]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Basic operations ==&lt;br /&gt;
Although most machines are not able to address individual bits in memory, nor have instructions to manipulate single bits, each bit in a word can be singled out and manipulated using [[bitwise operation]]s. In particular:&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;OR&amp;lt;/code&amp;gt; to set a bit to one:&lt;br /&gt;
       11101&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;10 &lt;br /&gt;
 OR 00000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;00 &lt;br /&gt;
    = 11101&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;10&lt;br /&gt;
&amp;lt;code&amp;gt;AND&amp;lt;/code&amp;gt; to set a bit to zero:&lt;br /&gt;
          111010&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;0&lt;br /&gt;
 AND 111111&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;1&lt;br /&gt;
       = 111010&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;0&lt;br /&gt;
&amp;lt;code&amp;gt;AND&amp;lt;/code&amp;gt; to determine if a bit is set, by zero-testing:&lt;br /&gt;
             1110101&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;                     111010&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;0&lt;br /&gt;
    AND 0000000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;            AND 000000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;0&lt;br /&gt;
          = 0000000&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;                  = 000000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;0&lt;br /&gt;
 (=0 ∴ bit isn&amp;#039;t set)     (≠0 ∴ bit is set)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;XOR&amp;lt;/code&amp;gt; to invert or toggle a bit:&lt;br /&gt;
         11101&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;10             11101&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;10&lt;br /&gt;
 XOR 00000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;00    XOR 00000&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;00&lt;br /&gt;
      = 11101&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;10         =  11101&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;10&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;NOT&amp;lt;/code&amp;gt; to invert all bits:&lt;br /&gt;
 NOT 10110010 &lt;br /&gt;
       = 01001101&lt;br /&gt;
&lt;br /&gt;
To obtain the [[Mask (computing)|bit mask]] needed for these operations, we can use a [[Bitwise operation#Bit shifts|bit shift]] operator to shift the number 1 to the left by the appropriate number of places, as well as [[bitwise negation]] if necessary.&lt;br /&gt;
&lt;br /&gt;
Given two bit arrays of the same size representing sets, we can compute their [[union (set theory)|union]], [[intersection (set theory)|intersection]], and [[complement (set theory)|set-theoretic difference]] using &amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;w&amp;#039;&amp;#039; simple bit operations each (2&amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;w&amp;#039;&amp;#039; for difference), as well as the [[Signed number representations#Ones&amp;#039; complement|complement]] of either:&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;for&amp;#039;&amp;#039;&amp;#039; i &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039; 0 &amp;#039;&amp;#039;&amp;#039;to&amp;#039;&amp;#039;&amp;#039; n/w-1&lt;br /&gt;
     complement_a[i] := &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; a[i]&lt;br /&gt;
     union[i]        := a[i] &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; b[i]&lt;br /&gt;
     intersection[i] := a[i] &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; b[i]&lt;br /&gt;
     difference[i]   := a[i] &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; b[i])&lt;br /&gt;
&lt;br /&gt;
If we wish to iterate through the bits of a bit array, we can do this efficiently using a doubly nested loop that loops through each word, one at a time. Only &amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;w&amp;#039;&amp;#039; memory accesses are required:&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;for&amp;#039;&amp;#039;&amp;#039; i &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039; 0 to n/w-1&lt;br /&gt;
     index := 0    // if needed&lt;br /&gt;
     word := a[i]&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;for&amp;#039;&amp;#039;&amp;#039; b &amp;#039;&amp;#039;&amp;#039;from&amp;#039;&amp;#039;&amp;#039; 0 to w-1&lt;br /&gt;
         value := word &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; 1 ≠ 0&lt;br /&gt;
         word := word shift right 1&lt;br /&gt;
         // do something with value&lt;br /&gt;
         index := index + 1    // if needed&lt;br /&gt;
&lt;br /&gt;
Both of these code samples exhibit ideal [[locality of reference]], which will subsequently receive large performance boost from a data cache. If a cache line is &amp;#039;&amp;#039;k&amp;#039;&amp;#039; words, only about &amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;wk&amp;#039;&amp;#039; cache misses will occur.&lt;br /&gt;
&lt;br /&gt;
== More complex operations ==&lt;br /&gt;
&lt;br /&gt;
As with [[String (computer science)|character strings]] it is straightforward to define &amp;#039;&amp;#039;length&amp;#039;&amp;#039;, &amp;#039;&amp;#039;substring&amp;#039;&amp;#039;, [[lexicographical order|lexicographical]] &amp;#039;&amp;#039;compare&amp;#039;&amp;#039;, &amp;#039;&amp;#039;concatenation&amp;#039;&amp;#039;, &amp;#039;&amp;#039;reverse&amp;#039;&amp;#039; operations. The implementation of some of these operations is sensitive to [[endianness]].&lt;br /&gt;
&lt;br /&gt;
=== Population / Hamming weight ===&lt;br /&gt;
If we wish to find the number of 1 bits in a bit array, sometimes called the population count or Hamming weight, there are efficient branch-free algorithms that can compute the number of bits in a word using a series of simple bit operations. We simply run such an algorithm on each word and keep a running total. Counting zeros is similar. See the [[Hamming weight]] article for examples of an efficient implementation.&lt;br /&gt;
&lt;br /&gt;
=== Inversion ===&lt;br /&gt;
&lt;br /&gt;
Vertical flipping of a one-bit-per-pixel image, or some FFT algorithms, requires flipping the bits of individual words (so &amp;lt;code&amp;gt;b31 b30 ... b0&amp;lt;/code&amp;gt; becomes &amp;lt;code&amp;gt;b0 ... b30 b31&amp;lt;/code&amp;gt;).&lt;br /&gt;
When this operation is not available on the processor, it&amp;#039;s still possible to proceed by successive passes, in this example on 32 bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
exchange two 16-bit halfwords&lt;br /&gt;
exchange bytes by pairs (0xddccbbaa -&amp;gt; 0xccddaabb)&lt;br /&gt;
...&lt;br /&gt;
swap bits by pairs&lt;br /&gt;
swap bits (b31 b30 ... b1 b0 -&amp;gt; b30 b31 ... b0 b1)&lt;br /&gt;
&lt;br /&gt;
The last operation can be written ((x&amp;amp;0x55555555) &amp;lt;&amp;lt; 1) | (x&amp;amp;0xaaaaaaaa) &amp;gt;&amp;gt; 1)).&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Find first one ===&lt;br /&gt;
The [[find first set]] or &amp;#039;&amp;#039;find first one&amp;#039;&amp;#039; operation identifies the index or position of the 1-bit with the smallest index in an array, and has widespread hardware support (for arrays not larger than a word) and efficient algorithms for its computation. When a [[priority queue]] is stored in a bit array, find first one can be used to identify the highest priority element in the queue. To expand a word-size &amp;#039;&amp;#039;find first one&amp;#039;&amp;#039; to longer arrays, one can find the first nonzero word and then run &amp;#039;&amp;#039;find first one&amp;#039;&amp;#039; on that word. The related operations &amp;#039;&amp;#039;find first zero&amp;#039;&amp;#039;, &amp;#039;&amp;#039;count leading zeros&amp;#039;&amp;#039;, &amp;#039;&amp;#039;count leading ones&amp;#039;&amp;#039;, &amp;#039;&amp;#039;count trailing zeros&amp;#039;&amp;#039;, &amp;#039;&amp;#039;count trailing ones&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;log base 2&amp;#039;&amp;#039; (see [[find first set]]) can also be extended to a bit array in a straightforward manner.&lt;br /&gt;
&lt;br /&gt;
== Compression ==&lt;br /&gt;
&lt;br /&gt;
A bit array is the most dense storage for &amp;quot;random&amp;quot; bits, that is, where each bit is equally likely to be 0 or 1, and each one is independent. But most data are not random, so it may be possible to store it more compactly. For example, the data of a typical fax image is not random and can be compressed. [[Run-length encoding]] is commonly used to compress these long streams. However, most compressed data formats are not so easy to access randomly; also by compressing bit arrays too aggressively we run the risk of losing the benefits due to bit-level parallelism ([[Array programming|vectorization]]). Thus, instead of compressing bit arrays as streams of bits, we might compress them as streams of bytes or words (see [[Bitmap index#Compression|Bitmap index (compression)]]).&lt;br /&gt;
&lt;br /&gt;
== Advantages and disadvantages ==&lt;br /&gt;
Bit arrays, despite their simplicity, have a number of marked advantages over other data structures for the same problems:&lt;br /&gt;
* They are extremely compact; no other data structures can store &amp;#039;&amp;#039;n&amp;#039;&amp;#039; independent pieces of data in &amp;#039;&amp;#039;n&amp;#039;&amp;#039;/&amp;#039;&amp;#039;w&amp;#039;&amp;#039; words.&lt;br /&gt;
* They allow small arrays of bits to be stored and manipulated in the register set for long periods of time with no memory accesses.&lt;br /&gt;
* Because of their ability to exploit bit-level parallelism, limit memory access, and maximally use the [[data cache]], they often outperform many other data structures on practical data sets, even those that are more asymptotically efficient.&lt;br /&gt;
However, bit arrays are not the solution to everything. In particular:&lt;br /&gt;
* Without compression, they are wasteful set data structures for sparse sets (those with few elements compared to their range) in both time and space. For such applications, compressed bit arrays, [[Judy array]]s, [[trie]]s, or even [[Bloom filter]]s should be considered instead.&lt;br /&gt;
* Accessing individual elements can be expensive and difficult to express in some languages. If random access is more common than sequential and the array is relatively small, a byte array may be preferable on a machine with byte addressing. A word array, however, is probably not justified due to the huge space overhead and additional cache misses it causes, unless the machine only has word addressing.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
Because of their compactness, bit arrays have a number of applications in areas where space or efficiency is at a premium. Most commonly, they are used to represent a simple group of Boolean flags or an ordered sequence of Boolean values.&lt;br /&gt;
&lt;br /&gt;
Bit arrays are used for [[priority queue]]s, where the bit at index &amp;#039;&amp;#039;k&amp;#039;&amp;#039; is set if and only if &amp;#039;&amp;#039;k&amp;#039;&amp;#039; is in the queue; this data structure is used, for example, by the [[Linux kernel]], and benefits strongly from a find-first-zero operation in hardware.&lt;br /&gt;
&lt;br /&gt;
Bit arrays can be used for the allocation of [[page (computing)|memory pages]], [[inode]]s, disk sectors, etc. In such cases, the term &amp;#039;&amp;#039;bitmap&amp;#039;&amp;#039; may be used. However, this term is frequently used to refer to [[raster graphics|raster images]], which may use multiple [[color depth|bits per pixel]].&lt;br /&gt;
&lt;br /&gt;
Another application of bit arrays is the [[Bloom filter]], a probabilistic [[set data structure]] that can store large sets in a small space in exchange for a small probability of error. It is also possible to build probabilistic [[hash table]]s based on bit arrays that accept either false positives or false negatives.&lt;br /&gt;
&lt;br /&gt;
Bit arrays and the operations on them are also important for constructing [[succinct data structure]]s, which use close to the minimum possible space. In this context, operations like finding the &amp;#039;&amp;#039;n&amp;#039;&amp;#039;th 1 bit or counting the number of 1 bits up to a certain position become important.&lt;br /&gt;
&lt;br /&gt;
Bit arrays are also a useful abstraction for examining streams of [[data compression|compressed]] data, which often contain elements that occupy portions of bytes or are not byte-aligned. For example, the compressed [[Huffman coding]] representation of a single 8-bit character can be anywhere from 1 to 255 bits long.&lt;br /&gt;
&lt;br /&gt;
In [[information retrieval]], bit arrays are a good representation for the [[posting list]]s of very frequent terms. If we compute the gaps between adjacent values in a list of strictly increasing integers and encode them using [[unary coding]], the result is a bit array with a 1 bit in the &amp;#039;&amp;#039;n&amp;#039;&amp;#039;th position if and only if &amp;#039;&amp;#039;n&amp;#039;&amp;#039; is in the list. The implied probability of a gap of &amp;#039;&amp;#039;n&amp;#039;&amp;#039; is 1/2&amp;lt;sup&amp;gt;&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;lt;/sup&amp;gt;. This is also the special case of [[Golomb coding]] where the parameter M is 1; this parameter is only normally selected when {{math|−log(2 − &amp;#039;&amp;#039;p&amp;#039;&amp;#039;) / log(1 − &amp;#039;&amp;#039;p&amp;#039;&amp;#039;) ≤ 1}}, or roughly the term occurs in at least 38% of documents.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Given a big file of [[IPv4]] addresses (more than 100 GB)  — we need to count unique addresses. If we use generic &amp;lt;code&amp;gt;map[string]bool&amp;lt;/code&amp;gt; — we will need more than 64 GB of [[Random-access memory|RAM]], so we use the &amp;#039;&amp;#039;&amp;#039;bit map&amp;#039;&amp;#039;&amp;#039;, in [[Go (programming language)|Go]]:&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
package main&lt;br /&gt;
&lt;br /&gt;
import (&lt;br /&gt;
	&amp;quot;bufio&amp;quot;&lt;br /&gt;
	&amp;quot;fmt&amp;quot;&lt;br /&gt;
	&amp;quot;math/bits&amp;quot;&lt;br /&gt;
	&amp;quot;os&amp;quot;&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
// bitsetSize is the number of bytes needed for 2^32 bits (512 MiB)&lt;br /&gt;
const bitsetSize = 1 &amp;lt;&amp;lt; 29&lt;br /&gt;
&lt;br /&gt;
func main() {&lt;br /&gt;
	file, err := os.Open(&amp;quot;ip_addresses&amp;quot;)&lt;br /&gt;
	if err != nil {&lt;br /&gt;
		fmt.Println(&amp;quot;Error opening file:&amp;quot;, err)&lt;br /&gt;
		return&lt;br /&gt;
	}&lt;br /&gt;
	defer file.Close()&lt;br /&gt;
&lt;br /&gt;
	bitset := [bitsetSize]byte{}&lt;br /&gt;
&lt;br /&gt;
	// Use a buffered scanner with a larger buffer&lt;br /&gt;
	scanner := bufio.NewScanner(file)&lt;br /&gt;
	const maxBuffer = 64 * 1024 // 64 KB buffer&lt;br /&gt;
	buf := make([]byte, 0, maxBuffer)&lt;br /&gt;
	scanner.Buffer(buf, maxBuffer)&lt;br /&gt;
&lt;br /&gt;
	// Process each line&lt;br /&gt;
	for scanner.Scan() {&lt;br /&gt;
		line := scanner.Bytes()&lt;br /&gt;
&lt;br /&gt;
		// Parse the IP address manually from bytes&lt;br /&gt;
		ip := parseIPv4(line)&lt;br /&gt;
		// Set the bit&lt;br /&gt;
		byteIndex := ip &amp;gt;&amp;gt; 3 // Divide by 8&lt;br /&gt;
		bitIndex := ip &amp;amp; 7   // Bit position 0-7&lt;br /&gt;
		bitset[byteIndex] |= 1 &amp;lt;&amp;lt; bitIndex&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Check for scanning errors&lt;br /&gt;
	if err := scanner.Err(); err != nil {&lt;br /&gt;
		fmt.Println(&amp;quot;Error reading file:&amp;quot;, err)&lt;br /&gt;
		return&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	var count uint64&lt;br /&gt;
	for i := 0; i &amp;lt; bitsetSize; i++ {&lt;br /&gt;
		count += uint64(bits.OnesCount8(bitset[i]))&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	fmt.Println(&amp;quot;Number of unique IPv4 addresses:&amp;quot;, count)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
func parseIPv4(line []byte) (ip uint32) {&lt;br /&gt;
	i := 0&lt;br /&gt;
&lt;br /&gt;
	// Octet 1&lt;br /&gt;
	n := uint32(line[i] - &amp;#039;0&amp;#039;)&lt;br /&gt;
	for i = 1; line[i] != &amp;#039;.&amp;#039;; i++ {&lt;br /&gt;
		n = n*10 + uint32(line[i]-&amp;#039;0&amp;#039;)&lt;br /&gt;
	}&lt;br /&gt;
	ip |= n &amp;lt;&amp;lt; 24&lt;br /&gt;
	i++ // Skip the dot&lt;br /&gt;
&lt;br /&gt;
	// Octet 2&lt;br /&gt;
	n = uint32(line[i] - &amp;#039;0&amp;#039;)&lt;br /&gt;
	i++&lt;br /&gt;
	for ; line[i] != &amp;#039;.&amp;#039;; i++ {&lt;br /&gt;
		n = n*10 + uint32(line[i]-&amp;#039;0&amp;#039;)&lt;br /&gt;
	}&lt;br /&gt;
	ip |= n &amp;lt;&amp;lt; 16&lt;br /&gt;
	i++ // Skip the dot&lt;br /&gt;
&lt;br /&gt;
	// Octet 3&lt;br /&gt;
	n = uint32(line[i] - &amp;#039;0&amp;#039;)&lt;br /&gt;
	i++&lt;br /&gt;
	for ; line[i] != &amp;#039;.&amp;#039;; i++ {&lt;br /&gt;
		n = n*10 + uint32(line[i]-&amp;#039;0&amp;#039;)&lt;br /&gt;
	}&lt;br /&gt;
	ip |= n &amp;lt;&amp;lt; 8&lt;br /&gt;
	i++ // Skip the dot&lt;br /&gt;
&lt;br /&gt;
	// Octet 4&lt;br /&gt;
	n = uint32(line[i] - &amp;#039;0&amp;#039;)&lt;br /&gt;
	i++&lt;br /&gt;
	for ; i &amp;lt; len(line); i++ {&lt;br /&gt;
		n = n*10 + uint32(line[i]-&amp;#039;0&amp;#039;)&lt;br /&gt;
	}&lt;br /&gt;
	ip |= n&lt;br /&gt;
&lt;br /&gt;
	return ip&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Language support ==&lt;br /&gt;
The [[APL (programming language)|APL programming language]] fully supports bit arrays of arbitrary shape and size as a Boolean datatype distinct from integers. All major implementations ([[APL (programming language)#Execution|Dyalog APL, APL2, APL Next, NARS2000, Gnu APL]], etc.) pack the bits densely into whatever size the machine word is. Bits may be accessed individually via the usual indexing notation (for example, &amp;lt;code&amp;gt;A[3]&amp;lt;/code&amp;gt;) as well as through all of the usual primitive functions and operators where they are often operated on using a special case algorithm such as summing the bits via a table lookup of bytes.&lt;br /&gt;
&lt;br /&gt;
The [[C (programming language)|C programming language]]&amp;#039;s &amp;#039;&amp;#039;[[bit field]]s&amp;#039;&amp;#039;, pseudo-objects found in structs with size equal to some number of bits, are in fact small bit arrays; they are limited in that they cannot span words. Although they give a convenient syntax, the bits are still accessed using bytewise operators on most machines, and they can only be defined statically (like C&amp;#039;s static arrays, their sizes are fixed at compile-time). It is also a common idiom for C programmers to use words as small bit arrays and access bits of them using bit operators. A widely available header file included in the [[X11]] system, &amp;lt;code&amp;gt;&amp;lt;xtrapbits.h&amp;gt;&amp;lt;/code&amp;gt;, is “a portable way for systems to define bit field manipulation of arrays of bits.” A more explanatory description of aforementioned approach can be found in the [http://c-faq.com/misc/bitsets.html comp.lang.c faq].&lt;br /&gt;
&lt;br /&gt;
In [[C++]], although individual &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;s typically occupy the same space as a byte or an integer, the [[Standard Template Library|STL]] type &amp;lt;code&amp;gt;std::vector&amp;lt;bool&amp;gt;&amp;lt;/code&amp;gt; is a [[partial template specialization]] in which bits are packed as a space efficiency optimization. Since bytes (and not bits) are the smallest addressable unit in C++, the &amp;lt;code&amp;gt;[]&amp;lt;/code&amp;gt; operator does &amp;#039;&amp;#039;not&amp;#039;&amp;#039; return a reference to an element, but instead returns a [[Proxy pattern|proxy reference]]. This might seem a minor point, but it means that &amp;lt;code&amp;gt;vector&amp;lt;bool&amp;gt;&amp;lt;/code&amp;gt; is &amp;#039;&amp;#039;not&amp;#039;&amp;#039; a standard STL container, which is why the use of &amp;lt;code&amp;gt;vector&amp;lt;bool&amp;gt;&amp;lt;/code&amp;gt; is generally discouraged. Another unique STL class, &amp;lt;code&amp;gt;std::bitset&amp;lt;/code&amp;gt;,&amp;lt;ref name=&amp;quot;c++&amp;quot; /&amp;gt; creates a vector of bits fixed at a particular size at compile-time, and in its interface and syntax more resembles the idiomatic use of words as bit sets by C programmers. It also has some additional power, such as the ability to efficiently count the number of bits that are set. Like {{mono|[[std::array|array]]}}, the size of &amp;lt;code&amp;gt;bitset&amp;lt;/code&amp;gt; must be specified at compile time, and cannot be inferred by the compiler. The [[Boost C++ Libraries]] provides a &amp;lt;code&amp;gt;boost::dynamic_bitset&amp;lt;/code&amp;gt; class&amp;lt;ref name=&amp;quot;boost&amp;quot; /&amp;gt; whose size is specified at run-time.&lt;br /&gt;
&lt;br /&gt;
The [[D programming language]] provides bit arrays in its standard library, Phobos, in &amp;lt;code&amp;gt;std.bitmanip&amp;lt;/code&amp;gt;. As in C++, the [] operator does not return a reference, since individual bits are not directly addressable on most hardware, but instead returns a &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In [[Java (programming language)|Java]], the class {{Javadoc:SE|java/util|BitSet}} (&amp;lt;code&amp;gt;java.util.BitSet&amp;lt;/code&amp;gt;) creates a bit array that is then manipulated with functions named after bitwise operators familiar to C programmers. Unlike the &amp;lt;code&amp;gt;bitset&amp;lt;/code&amp;gt; in C++, the Java &amp;lt;code&amp;gt;BitSet&amp;lt;/code&amp;gt; does not have a &amp;quot;size&amp;quot; state (it has an effectively infinite size, initialized with 0 bits); a bit can be set or tested at any index. In addition, there is a class {{Javadoc:SE|java/util|EnumSet}} (&amp;lt;code&amp;gt;java.util.EnumSet&amp;lt;/code&amp;gt;), which represents a Set of values of an [[enumerated type]] internally as a bit vector, as a safer alternative to bit fields.&lt;br /&gt;
&lt;br /&gt;
The [[.NET Framework]] supplies a &amp;lt;code&amp;gt;Systems.Collections.BitArray&amp;lt;/code&amp;gt; collection class. It stores bits using an array of type &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; (each element in the array usually represents 32 bits).&amp;lt;ref&amp;gt;{{cite web|url=https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/bitarray.cs|title=.NET mscorlib source code|website=github.com/microsoft|date=15 October 2021}}&amp;lt;/ref&amp;gt; The class supports random access and bitwise operators, can be iterated over, and its &amp;lt;code&amp;gt;Length&amp;lt;/code&amp;gt; property can be changed to grow or truncate it.&lt;br /&gt;
&lt;br /&gt;
Although [[Standard ML]] has no support for bit arrays, Standard ML of New Jersey has an extension, the &amp;lt;code&amp;gt;BitArray&amp;lt;/code&amp;gt; structure, in its SML/NJ Library. It is not fixed in size and supports set operations and bit operations, including, unusually, shift operations.&lt;br /&gt;
&lt;br /&gt;
[[Haskell (programming language)|Haskell]] likewise currently lacks standard support for bitwise operations, but both [[Glasgow Haskell Compiler|GHC]] and Hugs provide a &amp;lt;code&amp;gt;Data.Bits&amp;lt;/code&amp;gt; module with assorted bitwise functions and operators, including shift and rotate operations and an &amp;quot;unboxed&amp;quot; array over Boolean values may be used to model a Bit array, although this lacks support from the former module.&lt;br /&gt;
&lt;br /&gt;
In [[Perl]], strings can be used as expandable bit arrays. They can be manipulated using the usual bitwise operators (&amp;lt;code&amp;gt;~ | &amp;amp; ^&amp;lt;/code&amp;gt;),&amp;lt;ref&amp;gt;{{cite web|url=http://perldoc.perl.org/perlop.html#Bitwise-String-Operators|title=perlop - perldoc.perl.org|website=perldoc.perl.org}}&amp;lt;/ref&amp;gt; and individual bits can be tested and set using the &amp;#039;&amp;#039;vec&amp;#039;&amp;#039; function.&amp;lt;ref&amp;gt;{{cite web|url=http://perldoc.perl.org/functions/vec.html|title=vec - perldoc.perl.org|website=perldoc.perl.org}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[Ruby (programming language)|Ruby]], one can access (but not set) a bit of an integer (&amp;lt;code&amp;gt;Fixnum&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Bignum&amp;lt;/code&amp;gt;) using the bracket operator (&amp;lt;code&amp;gt;[]&amp;lt;/code&amp;gt;), as if it were an array of bits.&lt;br /&gt;
&lt;br /&gt;
In [[Rust (programming language)|Rust]] 1.3.0, there existed a &amp;lt;code&amp;gt;std::collections::BitSet&amp;lt;/code&amp;gt; object, however it was deprecated and removed for some reason. Instead, one must use the &amp;#039;&amp;#039;bit_set&amp;#039;&amp;#039; or &amp;#039;&amp;#039;bitvec&amp;#039;&amp;#039; libraries.&amp;lt;ref&amp;gt;{{Cite web|title=bit_set - Rust|url=https://docs.rs/bit-set/latest/bit_set/|website=docs.rs|date=16 July 2024}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|title=bitvec - Rust|url=https://docs.rs/bitvec/latest/bitvec/|website=docs.rs|date=10 July 2022}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apple&amp;#039;s [[Core Foundation]] library contains [https://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBitVectorRef/Reference/reference.html CFBitVector] and [https://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFMutableBitVectorRef/Reference/reference.html#//apple_ref/doc/uid/20001500 CFMutableBitVector] structures.&lt;br /&gt;
&lt;br /&gt;
[[PL/I]] supports arrays of &amp;#039;&amp;#039;bit strings&amp;#039;&amp;#039; of arbitrary length, which may be either fixed-length or varying. The array elements may be &amp;#039;&amp;#039;aligned&amp;#039;&amp;#039;&amp;amp;mdash; each element begins on a byte or word boundary&amp;amp;mdash; or &amp;#039;&amp;#039;unaligned&amp;#039;&amp;#039;&amp;amp;mdash; elements immediately follow each other with no padding.&lt;br /&gt;
&lt;br /&gt;
[[PL/pgSQL]] and PostgreSQL&amp;#039;s SQL support &amp;#039;&amp;#039;bit strings&amp;#039;&amp;#039; as native type. There are two SQL bit types: &amp;lt;code&amp;gt;bit(&amp;#039;&amp;#039;&amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;bit varying(&amp;#039;&amp;#039;&amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;)&amp;lt;/code&amp;gt;, where &amp;#039;&amp;#039;&amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; is a positive integer.&amp;lt;ref&amp;gt;{{Cite web|url=https://www.postgresql.org/docs/current/datatype-bit.html|title=8.10. Bit String Types|date=30 September 2021}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hardware description languages such as [[VHDL]], [[Verilog]], and [[SystemVerilog]] natively support bit vectors as these are used to model storage elements like [[Flip-flop (electronics)|flip-flops]], hardware busses and hardware signals in general. In hardware verification languages such as OpenVera, [[e (verification language)|&amp;#039;&amp;#039;e&amp;#039;&amp;#039;]] and [[SystemVerilog]], bit vectors are used to sample values from the hardware models, and to represent data that is transferred to hardware during simulations.&lt;br /&gt;
&lt;br /&gt;
[[Common Lisp]] provides multi-dimensional bit arrays. A one-dimensional &amp;lt;code&amp;gt;bit-vector&amp;lt;/code&amp;gt; implementation is provided as a special case of the built-in &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt;, acting in a dual capacity as a class and a type specifier.&amp;lt;ref&amp;gt;{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_bt_vec.htm|title=CLHS: System Class BIT-VECTOR|website=www.lispworks.com}}&amp;lt;/ref&amp;gt; Bit arrays (and thus bit vectors) relies on the general &amp;lt;code&amp;gt;make-array&amp;lt;/code&amp;gt; function to be configured with an element type of &amp;lt;code&amp;gt;bit&amp;lt;/code&amp;gt;, which optionally permits a bit vector to be designated as dynamically resizable. The &amp;lt;code&amp;gt;bit-vector&amp;lt;/code&amp;gt;, however, is not infinite in extent. A more restricted &amp;lt;code&amp;gt;simple-bit-vector&amp;lt;/code&amp;gt; type exists, which explicitly excludes the dynamic characteristics.&amp;lt;ref&amp;gt;{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_smp_bt.htm|title=CLHS: Type SIMPLE-BIT-VECTOR|website=www.lispworks.com}}&amp;lt;/ref&amp;gt; Bit vectors are represented as, and can be constructed in a more concise fashion by, the &amp;#039;&amp;#039;reader macro&amp;#039;&amp;#039; &amp;lt;code&amp;gt;#*&amp;lt;i&amp;gt;bits&amp;lt;/i&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;ref&amp;gt;{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/02_dhd.htm|title=CLHS: Section 2.4.8.4|website=www.lispworks.com}}&amp;lt;/ref&amp;gt; In addition to the general functions applicable to all arrays, dedicated operations exist for bit arrays. Single bits may be accessed and modified using the &amp;lt;code&amp;gt;bit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sbit&amp;lt;/code&amp;gt; functions&amp;lt;ref&amp;gt;{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/f_bt_sb.htm|title=CLHS: Accessor BIT, SBIT|website=www.lispworks.com}}&amp;lt;/ref&amp;gt; and an extensive number of logical operations is supported.&amp;lt;ref&amp;gt;{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/f_bt_and.htm|title=CLHS: Function BIT-AND, BIT-ANDC1, BIT-ANDC2...|website=www.lispworks.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Arithmetic logic unit]]&lt;br /&gt;
* [[Binary code]]&lt;br /&gt;
* [[Binary numeral system]]&lt;br /&gt;
* [[Bitboard]] Chess and similar games.&lt;br /&gt;
* [[Bit field]]&lt;br /&gt;
* [[Mask (computing)|Bit mask]]&lt;br /&gt;
* [[Bitmap index]]&lt;br /&gt;
* [[Bitstream]]&lt;br /&gt;
* [[Finite field]] of 2 elements, or [[GF(2)]]&lt;br /&gt;
* [[Judy array]]&lt;br /&gt;
* [[Variable-length code]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist | refs =&lt;br /&gt;
&amp;lt;ref name=&amp;quot;c++&amp;quot;&amp;gt;{{cite web|url=http://www.sgi.com/tech/stl/bitset.html|title=SGI.com Tech Archive Resources now retired|date=2 January 2018|publisher=[[Silicon Graphics|SGI]]}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;boost&amp;quot;&amp;gt;{{cite web|url=http://www.boost.org/libs/dynamic_bitset/dynamic_bitset.html|title=dynamic_bitset&amp;lt;Block, Allocator&amp;gt; - 1.66.0|website=www.boost.org}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://www-cs-faculty.stanford.edu/~knuth/fasc1a.ps.gz mathematical bases] {{Webarchive|url=https://web.archive.org/web/20191016044101/http://www-cs-faculty.stanford.edu/~knuth/fasc1a.ps.gz |date=2019-10-16 }} by Pr. D.E.Knuth&lt;br /&gt;
* [http://www.gotw.ca/publications/N1185.pdf vector&amp;amp;lt;bool&amp;amp;gt; Is Nonconforming, and Forces Optimization Choice]&lt;br /&gt;
* [http://www.gotw.ca/publications/N1211.pdf vector&amp;amp;lt;bool&amp;amp;gt;: More Problems, Better Solutions]&lt;br /&gt;
&lt;br /&gt;
{{Data structures}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Arrays]]&lt;br /&gt;
[[Category:Bit data structures]]&lt;/div&gt;</summary>
		<author><name>~2025-32447-81</name></author>
	</entry>
</feed>