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
Hamming weight
(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 == Some C compilers provide intrinsic functions that provide bit counting facilities. For example, [[GNU Compiler Collection|GCC]] (since version 3.4 in April 2004) includes a builtin function <code>__builtin_popcount</code> that will use a processor instruction if available or an efficient library implementation otherwise.<ref name="GCC"/> [[LLVM|LLVM-GCC]] has included this function since version 1.5 in June 2005.<ref name="LLVM"/> In the [[C++ Standard Library]], the bit-array data structure <code>bitset</code> has a <code>count()</code> method that counts the number of bits that are set. In [[C++20]], a new header <code><bit></code> was added, containing functions <code>std::popcount</code> and <code>std::has_single_bit</code>, taking arguments of unsigned integer types. In Java, the growable bit-array data structure {{Javadoc:SE|java/util|BitSet}} has a {{Javadoc:SE|java/util|BitSet|cardinality()}} method that counts the number of bits that are set. In addition, there are {{Javadoc:SE|java/lang|Integer|bitCount(int)}} and {{Javadoc:SE|java/lang|Long|bitCount(long)}} functions to count bits in primitive 32-bit and 64-bit integers, respectively. Also, the {{Javadoc:SE|java/math|BigInteger}} arbitrary-precision integer class also has a {{Javadoc:SE|java/math|BigInteger|bitCount()}} method that counts bits. In [[Python (programming language)|Python]], the <code>int</code> type has a <code>bit_count()</code> method to count the number of bits set. This functionality was introduced in Python 3.10, released in October 2021.<ref>{{cite web |title=What's New In Python 3.10 |url=https://docs.python.org/3.10/whatsnew/3.10.html |website=python.org}}</ref> In [[Common Lisp]], the function <code>logcount</code>, given a non-negative integer, returns the number of 1 bits. (For negative integers it returns the number of 0 bits in 2's complement notation.) In either case the integer can be a BIGNUM. Starting in [[Glasgow Haskell Compiler|GHC]] 7.4, the [[Haskell (programming language)|Haskell]] base package has a <code>popCount</code> function available on all types that are instances of the <code>Bits</code> class (available from the <code>Data.Bits</code> module).<ref name="GHC"/> [[MySQL]] version of [[SQL]] language provides <code>BIT_COUNT()</code> as a standard function.<ref name="MySQL_50"/> [[Fortran 2008]] has the standard, intrinsic, elemental function <code>popcnt</code> returning the number of nonzero bits within an integer (or integer array).<ref name="Metcalf_2011"/> Some programmable scientific pocket calculators feature special commands to calculate the number of set bits, e.g. <code>#B</code> on the [[HP-16C]]<ref name="HP-16C_1982"/><ref name="Schwartz_Grevelle_2003"/> and [[WP 43S]],<ref name="Bonin_2019_OG"/><ref name="Bonin_2019_RG"/> <code>#BITS</code><ref name="Martin_McClure_2015"/><ref name="Martin_2015"/> or <code>BITSUM</code><ref name="Thörngren_2017"/><ref name="Thörngren_2017_2"/> on HP-16C emulators, and <code>nBITS</code> on the [[WP 34S]].<ref name="Bonin_2012"/><ref name="Bonin_2015"/> [[FreePascal]] implements popcnt since version 3.0.<ref name="FPC docs"/>
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)