summaryrefslogtreecommitdiffstats
path: root/Objects/clinic/longobject.c.h
diff options
context:
space:
mode:
authorNiklas Fiekas <niklas.fiekas@backscattering.de>2020-05-29 16:28:02 (GMT)
committerGitHub <noreply@github.com>2020-05-29 16:28:02 (GMT)
commit8bd216dfede9cb2d5bedb67f20a30c99844dbfb8 (patch)
tree0cae604e76e929467bba3cb1ebbec3ac8a417947 /Objects/clinic/longobject.c.h
parent364b5ead1584583db91ef7f9d9f87f01bfbb5774 (diff)
downloadcpython-8bd216dfede9cb2d5bedb67f20a30c99844dbfb8.zip
cpython-8bd216dfede9cb2d5bedb67f20a30c99844dbfb8.tar.gz
cpython-8bd216dfede9cb2d5bedb67f20a30c99844dbfb8.tar.bz2
bpo-29882: Add an efficient popcount method for integers (#771)
* bpo-29882: Add an efficient popcount method for integers * Update 'sign bit' and versionadded in docs * Add entry to whatsnew document * Doc: use positive example, mention population count * Minor cleanups of the core code * Move popcount_digit closer to where it's used * Use z instead of self after conversion * Add 'absolute value' and 'population count' to docstring * Fix clinic error about missing summary line * Ensure popcount_digit is portable with 64-bit ints Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Diffstat (limited to 'Objects/clinic/longobject.c.h')
-rw-r--r--Objects/clinic/longobject.c.h27
1 files changed, 26 insertions, 1 deletions
diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h
index 7db8965..cf388c5 100644
--- a/Objects/clinic/longobject.c.h
+++ b/Objects/clinic/longobject.c.h
@@ -138,6 +138,31 @@ int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored))
return int_bit_length_impl(self);
}
+PyDoc_STRVAR(int_bit_count__doc__,
+"bit_count($self, /)\n"
+"--\n"
+"\n"
+"Number of ones in the binary representation of the absolute value of self.\n"
+"\n"
+"Also known as the population count.\n"
+"\n"
+">>> bin(13)\n"
+"\'0b1101\'\n"
+">>> (13).bit_count()\n"
+"3");
+
+#define INT_BIT_COUNT_METHODDEF \
+ {"bit_count", (PyCFunction)int_bit_count, METH_NOARGS, int_bit_count__doc__},
+
+static PyObject *
+int_bit_count_impl(PyObject *self);
+
+static PyObject *
+int_bit_count(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return int_bit_count_impl(self);
+}
+
PyDoc_STRVAR(int_as_integer_ratio__doc__,
"as_integer_ratio($self, /)\n"
"--\n"
@@ -308,4 +333,4 @@ skip_optional_kwonly:
exit:
return return_value;
}
-/*[clinic end generated code: output=63b8274fc784d617 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=4257cfdb155efd00 input=a9049054013a1b77]*/