You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Language/Structure/Bitwise Operators/bitshiftRight.adoc
+10-10
Original file line number
Diff line number
Diff line change
@@ -29,8 +29,8 @@ The right shift operator `>>` causes the bits of the left operand to be shifted
29
29
30
30
[float]
31
31
=== 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`.
34
34
35
35
--
36
36
// OVERVIEW SECTION ENDS
@@ -47,29 +47,29 @@ The right shift operator `>>` causes the bits of the left operand to be shifted
47
47
[source,arduino]
48
48
----
49
49
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
51
51
----
52
52
[%hardbreaks]
53
53
54
54
[float]
55
55
=== 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`.
57
57
58
58
[source,arduino]
59
59
----
60
-
int x = -16; // binary: 1111111111110000
60
+
int x = -16; // binary: 1111111111110000
61
61
int y = 3;
62
-
int result = x >> y; // binary: 1111111111111110
62
+
int result = x >> y; // binary: 1111111111111110, decimal: -2
63
63
----
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:
65
65
66
66
[source,arduino]
67
67
----
68
-
int x = -16; // binary: 1111111111110000
68
+
int x = -16; // binary: 1111111111110000
69
69
int y = 3;
70
-
int result = (unsigned int)x >> y; // binary: 0001111111111110
70
+
int result = (unsigned int)x >> y; // binary: 0001111111111110, decimal: 8190
71
71
----
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:
0 commit comments