# Function for counting number of set bits?

**URL:** <https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600>\
**Category:** General Usage\
**Created:** [January 25, 2018, 6:57pm UTC](https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600 "2018-01-25T18:57:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [January 25, 2018, 6:57pm UTC](https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600/1 "2018-01-25T18:57:18Z")

</div>

i.e. the number of `1` bits in a bits-type. I read somewhere that this is actually an x86 instruction, so if that’s the case, I’d very much like to use that. I can’t seem to find this in the documentation…

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [January 25, 2018, 7:07pm UTC](https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600/2 "2018-01-25T19:07:35Z")

</div>

`count_ones` is your friend 🙂

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [January 25, 2018, 7:09pm UTC](https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600/3 "2018-01-25T19:09:50Z")

</div>

Oh the irony, considering the debate raging in the (probably soon to be infamous) underscores thread 😆

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 25, 2018, 8:59pm UTC](https://discourse.julialang.org/t/function-for-counting-number-of-set-bits/8600/4 "2018-01-25T20:59:24Z")

</div>

Note that the Julia contributing guide clearly states that method names should be lower case with underscores! (That part was added by Jake Bolewski, back 3 years ago, in [#9848](https://github.com/JuliaLang/julia/pull/9848))  
Nobody disagreed, the only discussion on the PR was about the choice of 92 characters per line!
