2

What's the name of the function that tells you how many bits are set in some variable? This surely already exists in Base or maybe some standard library.

dan
  • 4,262
  • 7
  • 25
  • 40

1 Answers1

3

To quote Keno Fischer...

Try count_ones. As you can see it uses the popcnt instruction:

julia> code_native(count_ones,(Int64,))
    .section    __TEXT,__text,regular,pure_instructions
Filename: int.jl
Source line: 192
    push    RBP
    mov RBP, RSP
Source line: 192
    popcnt  RAX, RDI
    pop RBP
    ret

Is your question in any way related to the Hacker News buzz about Replacing a 32-bit loop count variable with 64-bit introduces crazy performance deviations?

Community
  • 1
  • 1
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
  • Hey, your answer as edited is clearly acceptable since it has been edited by a moderator, but you recommended I learn from meta, so I found this: “Provide a link to the original page or answer”, and “Quote only the relevant portion”. HTH. http://meta.stackexchange.com/help/referencing – Pascal Cuoq Aug 15 '14 at 16:41
  • My concerns are the same as those expressed in this Meta question (thanks for recommending I educate myself there): http://meta.stackoverflow.com/questions/270522/helping-users-who-are-unable-unwilling-to-google – Pascal Cuoq Sep 03 '14 at 10:31