# xarray.ufuncs.bitwise_count

### xarray.ufuncs.bitwise_count *= <xarray.ufuncs._unary_ufunc object>*

xarray specific variant of `numpy.bitwise_count()`. Handles xarray objects by dispatching to the appropriate function for the underlying array type.

Documentation from numpy:

Computes the number of 1-bits in the absolute value of `x`.
Analogous to the builtin int.bit_count or `popcount` in C++.

* **Parameters:**
  * **x** ([array_like](https://numpy.org/doc/stable/glossary.html#term-array_like), `unsigned int`) – Input array.
  * **out** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray), [`None`](https://docs.python.org/3/builtins/constants.html#None), or [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray) and [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – A location into which the result is stored. If provided, it must have
    a shape that the inputs broadcast to. If not provided or None,
    a freshly-allocated array is returned. A tuple (possible only as a
    keyword argument) must have length equal to the number of outputs.
  * **where** ([array_like](https://numpy.org/doc/stable/glossary.html#term-array_like), *optional*) – This condition is broadcast over the input. At locations where the
    condition is True, the out array will be set to the ufunc result.
    Elsewhere, the out array will retain its original value.
    Note that if an uninitialized out array is created via the default
    `out=None`, locations within it where the condition is False will
    remain uninitialized.
  * **\*\*kwargs** – For other keyword-only arguments, see the
    [ufunc docs](https://numpy.org/doc/stable/reference/ufuncs.html#ufuncs-kwargs).
* **Returns:**
  **y** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – The corresponding number of 1-bits in the input.
  Returns uint8 for all integer types
  This is a scalar if x is a scalar.

### References

1. [https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel](https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
2. Wikipedia, “Hamming weight”,
   [https://en.wikipedia.org/wiki/Hamming_weight](https://en.wikipedia.org/wiki/Hamming_weight)
3. [http://aggregate.ee.engr.uky.edu/MAGIC/#Population%20Count%20(Ones%20Count](http://aggregate.ee.engr.uky.edu/MAGIC/#Population%20Count%20(Ones%20Count))

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('0134f3a1-d8d4-4e4b-ae0e-1b1781afd78c','dba36aa4-d44c-42d4-969d-84dffd432f8c','cfb7df5c-9d68-46d1-a278-20689b4fb8d9','../lite/tree/../notebooks/index.html?path=2c2bfdbb_9531_448c_89ed_8c008bd42e55.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> import numpy as np
>>> np.bitwise_count(1023)
np.uint8(10)
>>> a = np.array([2**i - 1 for i in range(16)])
>>> np.bitwise_count(a)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
      dtype=uint8)
```

<div id="cfb7df5c-9d68-46d1-a278-20689b4fb8d9" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('0134f3a1-d8d4-4e4b-ae0e-1b1781afd78c','cfb7df5c-9d68-46d1-a278-20689b4fb8d9')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('0134f3a1-d8d4-4e4b-ae0e-1b1781afd78c','cfb7df5c-9d68-46d1-a278-20689b4fb8d9')">Open In Tab</button></div><div id="dba36aa4-d44c-42d4-969d-84dffd432f8c" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
