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

Expand docs for bitshift operators #14790

Merged
merged 1 commit into from Jan 30, 2016
Merged

Expand docs for bitshift operators #14790

merged 1 commit into from Jan 30, 2016

Conversation

simonbyrne
Copy link
Contributor

No description provided.

@simonbyrne simonbyrne added the doc This change adds or pertains to documentation label Jan 25, 2016
@@ -2082,7 +2082,11 @@ kron
"""
>>(x, n)

Right bit shift operator, preserving the sign of `x`.
Signed right bit shift operator, `x >> n`. The result is `x` shifted right by `n` bits,
Copy link
Contributor

Choose a reason for hiding this comment

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

>> is not always signed; it is signed for signed types, and unsigned for unsigned types.

That is, unsigned types always shift in zeros from the left.

The result is not undefined for negative n; there is generally very little undefined behaviour in Julia. Instead, n is reinterpreted as unsigned number. (This could/should/will change, leading to an error instead in this case.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point: I hadn't noticed that the code in intrinsics.cpp used an unsigned comparison. However, that's not quite true: big(1) << -1 returns an error, not big(1) << unsigned(-1) (which would be huge).

@simonbyrne
Copy link
Contributor Author

I've updated based on @eschnett's comments. I've left out the behaviour for n < 0 until #14516/#14791 is resolved.

@eschnett
Copy link
Contributor

LGTM.

@@ -6225,7 +6227,8 @@ nextind
"""
>>>(x, n)

Unsigned right bit shift operator.
Unsigned right bit shift operator, `x >>> n`. The result is `x` shifted right by `n` bits,
where `n >= 0`, filling with `0`s. This is equivalent to `(unsigned(x) >> n) % T`.
Copy link
Contributor

Choose a reason for hiding this comment

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

where did T come from?

please run genstdlib here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch.

Actually, should these be moved inline? If so, would they be better in int.jl or operators.jl?

Copy link
Contributor

Choose a reason for hiding this comment

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

may as well move them inline, yeah. I think operators.jl has the most general signature, but don't think it matters too much

@hayd
Copy link
Member

hayd commented Jan 26, 2016

I feel like an example may help here, e.g.

julia> 3 << 2
12

julia> bits(3)
"0000000000000000000000000000000000000000000000000000000000000011"

julia> bits(12)
"0000000000000000000000000000000000000000000000000000000000001100"

but that may be a little too verbose?

@simonbyrne
Copy link
Contributor Author

Good idea. The only problem is that the exact output will depend on the exact type of Int.

@KristofferC
Copy link
Sponsor Member

Could perhaps make it shorter and platform independent by specifying the int type:

julia> Int8(3) << 2
12

julia> bits(Int8(3))
"00000011"

julia> bits(Int8(12))
"00001100"

@simonbyrne
Copy link
Contributor Author

@KristofferC that's a good idea, makes it much more readable.

@simonbyrne simonbyrne force-pushed the sb/doc-shift branch 2 times, most recently from d4bb9d8 to 734d737 Compare January 26, 2016 09:12
Unsigned right bit shift operator, `x >>> n`. The result is `x` shifted right by `n` bits,
where `n >= 0`, filling with `0`s.

`BigInt`s are treated as if having infinite size, so no filling is required and this
Copy link
Member

Choose a reason for hiding this comment

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

Minor: I wonder if the example should go before the bigint note?

This is a big improvement, looks great!

@eschnett
Copy link
Contributor

This is a good change.

I like it if functions contain pointers to other, related functions. You could add a "See also: >>, >>>" to the documentation of <<, and vice versa.

hayd added a commit that referenced this pull request Jan 30, 2016
Expand docs for bitshift operators
@hayd hayd merged commit 4d1bf92 into master Jan 30, 2016
@hayd hayd deleted the sb/doc-shift branch January 30, 2016 07:02
"""
Base.(:(>>))
fieldoffsets
Copy link
Contributor

Choose a reason for hiding this comment

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

incorrectly resolved conflict here? fieldoffsets was recently deprecated

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I figured that was intentional. PR to fix below/soon.

hayd added a commit to hayd/julia that referenced this pull request Jan 30, 2016
This was accidentally added in JuliaLang#14790.
hayd added a commit to hayd/julia that referenced this pull request Jan 31, 2016
This was accidentally added in JuliaLang#14790.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants