Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add population count (number or 1 bits) function to int or int32-int64 #5798

Closed
DartBot opened this issue Oct 11, 2012 · 11 comments
Closed

Add population count (number or 1 bits) function to int or int32-int64 #5798

DartBot opened this issue Oct 11, 2012 · 11 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Oct 11, 2012

This issue was originally filed by @ahmetaa


This is a minor request. There are cases we need to count 1 bits in an integer. Something like:
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#bitCount(int)

@lrhn
Copy link
Member

lrhn commented Oct 11, 2012

Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Oct 12, 2012

I don't think it will make sense in general for int. As an arbitrary precision two's complement number, it doesn't have a finite bit representation (negative numbers conceptually have an infinite number of '1' bits, which can be seen in bitwise operations).

If there was a class, say Int32, for some finite representation integers, the bitCount operation would make sense on that. I don't really want to add operations on int that act on a limited number of bits, so int.countBits32() doesn't seem right.

That's just speculations, though. We'll think about it, but no promises.

@rakudrama
Copy link
Member

I think bitCount would be fine if it returned a special value for negative numbers.
I suggest null or -1 so the return type could be int, but double.Infinity would be ok.
Or it could throw an exception, like integer % by zero on the VM.

I don't think we should force people to use some Int32 class when, for some reasonable fraction of programs, in practice all the values are non-negative.

You can write (x & 0xFFFFFFFF).bitCount to always be sure of an int in the range 0..32, and that idiom is easily adapted to bit fields and numbers of bits that are unlikely to have a class, e.g. 17.

@rakudrama
Copy link
Member

Or like logcount in common lisp, count the zeroes in negative numbers.

@DartBot
Copy link
Author

DartBot commented Nov 8, 2012

This comment was originally written by @mdakin


I think expecting programmers to use (x & 0xFFFFFFFF) would be error prone.

How about providing .asUnsigned(n) even maybe .asUnsigned32() and .asUnsigned64() etc. methods? (as mentioned in http://code.google.com/p/dart/issues/detail?id=6486)

Or adding all good variations like bitCount(), bitCount32() and bitCount64() to int?

Another option would be having a class like Bits and move all methods there. But this one is not good.

@floitschG
Copy link
Contributor

Issue #10212 has been merged into this issue.

@floitschG
Copy link
Contributor

Under consideration, but not in the near future.


Added this to the Later milestone.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@srawlins
Copy link
Member

Is this still desirable, after pkg/math was scrapped? Would this go into int still?

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Sep 24, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-n and removed core-m labels Aug 29, 2017
@bkonyi
Copy link
Contributor

bkonyi commented Jun 27, 2018

I doubt we'll end up moving forward with this given that it's relatively simple to implement for users, so I'm closing this for now.

@bkonyi bkonyi closed this as completed Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

8 participants