Skip to content

Commit c3d6ad9

Browse files
authored
Merge pull request #928 from edgar-bonet/bit-shift
Multiple fixes to bitshiftRight.adoc
2 parents 70c4f62 + 35878f2 commit c3d6ad9

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Language/Structure/Bitwise Operators/bitshiftRight.adoc

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ The right shift operator `>>` causes the bits of the left operand to be shifted
2929

3030
[float]
3131
=== Parameters
32-
`variable`: Allowed data types: `byte`, `int`, `long`. +
33-
`number_of_bits`: a number that is < = 32. Allowed data types: `int`.
32+
`variable`: Allowed data types: any integer type (`byte`, `short`, `int`, `long`, `unsigned short`...). +
33+
`number_of_bits`: a positive number smaller than the bit-width of `variable`. Allowed data types: `int`.
3434

3535
--
3636
// OVERVIEW SECTION ENDS
@@ -47,29 +47,29 @@ The right shift operator `>>` causes the bits of the left operand to be shifted
4747
[source,arduino]
4848
----
4949
int a = 40; // binary: 0000000000101000
50-
int b = a >> 3; // binary: 0000000000000101, or 5 in decimal
50+
int b = a >> 3; // binary: 0000000000000101, decimal: 5
5151
----
5252
[%hardbreaks]
5353

5454
[float]
5555
=== Notes and Warnings
56-
When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons:
56+
When you shift `x` right by `y` bits (`x >> y`), the `y` rightmost bits of `x` “fall off” and are discarded. If `x` has an unsigned type (e.g. `unsigned int`), the `y` leftmost bits of the result are filled with zeroes. If `x` has a signed type (e.g. `int`), its leftmost bit is the sign bit, which determines whether it is positive or negative. In this case, the `y` leftmost bits of the result are filled with copies of the sign bit. This behavior, called “sign extension”, ensures the result has the same sign as `x`.
5757

5858
[source,arduino]
5959
----
60-
int x = -16; // binary: 1111111111110000
60+
int x = -16; // binary: 1111111111110000
6161
int y = 3;
62-
int result = x >> y; // binary: 1111111111111110
62+
int result = x >> y; // binary: 1111111111111110, decimal: -2
6363
----
64-
This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left:
64+
This may not be the behavior you want. If you instead want zeros to be shifted in from the left, you can use a typecast to suppress sign extension:
6565

6666
[source,arduino]
6767
----
68-
int x = -16; // binary: 1111111111110000
68+
int x = -16; // binary: 1111111111110000
6969
int y = 3;
70-
int result = (unsigned int)x >> y; // binary: 0001111111111110
70+
int result = (unsigned int)x >> y; // binary: 0001111111111110, decimal: 8190
7171
----
72-
Sign extension causes that you can use the right-shift operator `>>` as a way to divide by powers of 2, even negative numbers. For example:
72+
Sign extension causes the right-shift operator `>>` to perform a division by powers of 2, even with negative numbers. For example:
7373

7474
[source,arduino]
7575
----

0 commit comments

Comments
 (0)