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!
== Language support == 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 (A[3]) 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. The [[C (programming language)|C programming language]]'s ''[[bit field]]s'', 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'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, xtrapbits.h, 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]. In [[C++]], although individual <code>bool</code>s typically occupy the same space as a byte or an integer, the [[Standard Template Library|STL]] type <code>vector<bool></code> 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 [] operator does ''not'' return a reference to an element, but instead returns a [[Proxy pattern|proxy reference]]. This might seem a minor point, but it means that <code>vector<bool></code> is ''not'' a standard STL container, which is why the use of <code>vector<bool></code> is generally discouraged. Another unique STL class, <code>bitset</code>,<ref name="c++" /> 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. The [[Boost C++ Libraries]] provide a <code>dynamic_bitset</code> class<ref name="boost" /> whose size is specified at run-time. The [[D programming language]] provides bit arrays in its standard library, Phobos, in <code>std.bitmanip</code>. As in C++, the [] operator does not return a reference, since individual bits are not directly addressable on most hardware, but instead returns a <code>bool</code>. In [[Java (programming language)|Java]], the class {{Javadoc:SE|java/util|BitSet}} creates a bit array that is then manipulated with functions named after bitwise operators familiar to C programmers. Unlike the <code>bitset</code> in C++, the Java <code>BitSet</code> does not have a "size" 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}}, which represents a Set of values of an [[enumerated type]] internally as a bit vector, as a safer alternative to bit fields. The [[.NET Framework]] supplies a <code>BitArray</code> collection class. It stores bits using an array of type <code>int</code> (each element in the array usually represents 32 bits).<ref>{{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}}</ref> The class supports random access and bitwise operators, can be iterated over, and its <code>Length</code> property can be changed to grow or truncate it. Although [[Standard ML]] has no support for bit arrays, Standard ML of New Jersey has an extension, the <code>BitArray</code> structure, in its SML/NJ Library. It is not fixed in size and supports set operations and bit operations, including, unusually, shift operations. [[Haskell (programming language)|Haskell]] likewise currently lacks standard support for bitwise operations, but both [[Glasgow Haskell Compiler|GHC]] and Hugs provide a <code>Data.Bits</code> module with assorted bitwise functions and operators, including shift and rotate operations and an "unboxed" array over Boolean values may be used to model a Bit array, although this lacks support from the former module. In [[Perl]], strings can be used as expandable bit arrays. They can be manipulated using the usual bitwise operators (<code>~ | & ^</code>),<ref>{{cite web|url=http://perldoc.perl.org/perlop.html#Bitwise-String-Operators|title=perlop - perldoc.perl.org|website=perldoc.perl.org}}</ref> and individual bits can be tested and set using the ''vec'' function.<ref>{{cite web|url=http://perldoc.perl.org/functions/vec.html|title=vec - perldoc.perl.org|website=perldoc.perl.org}}</ref> In [[Ruby (programming language)|Ruby]], you can access (but not set) a bit of an integer (<code>Fixnum</code> or <code>Bignum</code>) using the bracket operator (<code>[]</code>), as if it were an array of bits. Apple'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. [[PL/I]] supports arrays of ''bit strings'' of arbitrary length, which may be either fixed-length or varying. The array elements may be ''aligned''— each element begins on a byte or word boundary— or ''unaligned''— elements immediately follow each other with no padding. [[PL/pgSQL]] and PostgreSQL's SQL support ''bit strings'' as native type. There are two SQL bit types: <code>bit(''<code>n</code>'')</code> and <code>bit varying(''<code>n</code>'')</code>, where ''<code>n</code>'' is a positive integer.<ref>{{Cite web|url=https://www.postgresql.org/docs/current/datatype-bit.html|title=8.10. Bit String Types|date=30 September 2021}}</ref> 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)|''e'']] and [[SystemVerilog]], bit vectors are used to sample values from the hardware models, and to represent data that is transferred to hardware during simulations. [[Common Lisp]] provides multi-dimensional bit arrays. A one-dimensional <code>bit-vector</code> implementation is provided as a special case of the built-in <code>array</code>, acting in a dual capacity as a class and a type specifier.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_bt_vec.htm|title=CLHS: System Class BIT-VECTOR|website=www.lispworks.com}}</ref> Bit arrays (and thus bit vectors) relies on the general <code>make-array</code> function to be configured with an element type of <code>bit</code>, which optionally permits a bit vector to be designated as dynamically resizable. The <code>bit-vector</code>, however, is not infinite in extent. A more restricted <code>simple-bit-vector</code> type exists, which explicitly excludes the dynamic characteristics.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/t_smp_bt.htm|title=CLHS: Type SIMPLE-BIT-VECTOR|website=www.lispworks.com}}</ref> Bit vectors are represented as, and can be constructed in a more concise fashion by, the ''reader macro'' <code>#*<i>bits</i></code>.<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/02_dhd.htm|title=CLHS: Section 2.4.8.4|website=www.lispworks.com}}</ref> 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 <code>bit</code> and <code>sbit</code> functions<ref>{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Body/f_bt_sb.htm|title=CLHS: Accessor BIT, SBIT|website=www.lispworks.com}}</ref> and an extensive number of logical operations is supported.<ref>{{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}}</ref>
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)