-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
Weird... I don't see the overflow running on my Mac. |
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. |
Good point! |
Maybe :-) |
Specs fail cause of overflow. |
@RX14 Yeah, any pointers on how to ignore that? |
@stakach the overflow is in the new code: https://github.com/crystal-lang/crystal/pull/8165/files#diff-cf769fe2e361e14888004d0eae8368fdR366 Try building with If you need wrapping around operations for |
The code looks code, the method is fancy and we could merge it. I just like to know what use cases this has. |
It's useful for dealing with binary data structures. 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 |
Would it be worth changing the PR title prior to merging to avoid confusion
as to what was actually added?
…On Thu, Sep 19, 2019, 5:41 PM Stephen von Takach ***@***.***> wrote:
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
extracts 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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8165?email_source=notifications&email_token=AAM4YSKWQK6WBBR3KNM4WZLQKP5Z3A5CNFSM4IUYAOIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7FA2AA#issuecomment-533335296>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAM4YSL5T7WMU3G67VZVKIDQKP5Z3ANCNFSM4IUYAOIA>
.
|
Int#[]
for accessing bit rangesInt#bits
for accessing bit ranges
Ruby 2.7 now has the original syntax used in this PR (without the |
I am so glad to be programming crystal as my primary language: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @stakach 🙏
inspired by https://twitter.com/iximeow/status/1170530444112097282?s=21