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 Int#bits for accessing bit ranges #8165

Merged
merged 3 commits into from
Oct 29, 2019
Merged

Add Int#bits for accessing bit ranges #8165

merged 3 commits into from
Oct 29, 2019

Conversation

stakach
Copy link
Contributor

@stakach stakach commented Sep 9, 2019

@stakach
Copy link
Contributor Author

stakach commented Sep 9, 2019

Weird... I don't see the overflow running on my Mac.
Any hints on how I should ignore that?

@asterite
Copy link
Member

asterite commented Sep 9, 2019

In the past we decided to explicitly not add this. Imagine passing an integer, or an array size, or whatever number to a method that you expect is something that indexes an array. Then your code compiles and you have no idea why. It can be really confusing and I experienced that in Ruby. We have the bit method for this.

@stakach
Copy link
Contributor Author

stakach commented Sep 9, 2019

Good point!
What about a #bits function?

@asterite
Copy link
Member

asterite commented Sep 9, 2019

What about a #bits function?

Maybe :-)

@RX14
Copy link
Contributor

RX14 commented Sep 17, 2019

Specs fail cause of overflow.

@stakach
Copy link
Contributor Author

stakach commented Sep 17, 2019

@RX14 Yeah, any pointers on how to ignore that?

src/int.cr Show resolved Hide resolved
@bcardiff
Copy link
Member

@stakach the overflow is in the new code: https://github.com/crystal-lang/crystal/pull/8165/files#diff-cf769fe2e361e14888004d0eae8368fdR366

Try building with -Dpreview_overflow and it should raise on your side aswell.

If you need wrapping around operations for + - *, use &+, &-, &*.

@asterite
Copy link
Member

The code looks code, the method is fancy and we could merge it. I just like to know what use cases this has.

@stakach
Copy link
Contributor Author

stakach commented Sep 19, 2019

It's useful for dealing with binary data structures.
https://en.wikipedia.org/wiki/IPv4#/media/File:Ipv4_address.svg

IPv4 is probably not a great example, but I've had to do things like extract the components of a custom floating point number format. So many weird and wonderful protocols out there where this is more descriptive and less error prone than bit shifting and masking.

i.e. https://github.com/lifeemotions/knx.net/blob/master/src/KNXLib/DPT/DataPoint2ByteFloatTemperature.cs#L28
How ugly is that code

@refi64
Copy link
Contributor

refi64 commented Sep 20, 2019 via email

@stakach stakach changed the title Add Int#[] for accessing bit ranges Add Int#bits for accessing bit ranges Sep 20, 2019
@icy-arctic-fox
Copy link
Contributor

Ruby 2.7 now has the original syntax used in this PR (without the #bits method). https://blog.saeloun.com/2019/09/24/ruby-2-7-integer-with-range.html

@stakach
Copy link
Contributor Author

stakach commented Sep 25, 2019

I am so glad to be programming crystal as my primary language:
ruby/ruby@6bedbf4

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @stakach 🙏

@RX14 RX14 added this to the 0.32.0 milestone Oct 29, 2019
@RX14 RX14 merged commit fe2bb68 into crystal-lang:master Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants