File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -1147,9 +1147,26 @@ arguments.
1147
1147
1148
1148
multi sub prefix:<+^>(Any --> Int:D)
1149
1149
1150
- X < Integer bitwise negation operator > : Coerces the argument to L < Int|/type/Int >
1151
- and does a bitwise negation on the result, assuming
1152
- L < two's complement|https://en.wikipedia.org/wiki/Two%27s_complement > .
1150
+ X < Integer bitwise negation operator > : converts the number to binary using as
1151
+ many bytes as needed by the number plus one; flips all bits and returns the
1152
+ result assuming it is a
1153
+ L < two's complement|https://en.wikipedia.org/wiki/Two%27s_complement >
1154
+ representation.
1155
+
1156
+ = for code
1157
+ say +^255; # OUTPUT: «-256»
1158
+
1159
+ In this case, 255 is 11111111 and would need a single byte. We use the
1160
+ representation in bytes needed for this value plus one, converting it to 0000
1161
+ 0000 1111 1111. Bitwise negation turns it into 1111 1111 0000 0000 and this
1162
+ is the representation in two's complement of -256, which is returned.
1163
+
1164
+ = for code
1165
+ say +^1; # OUTPUT: «-2»
1166
+ say +^(-256); # OUTPUT: «255»
1167
+
1168
+ Negative numbers are assumed to be represented as two's complements, and thus
1169
+ circle back to the original number.
1153
1170
1154
1171
= head2 prefix C « ~^ »
1155
1172
You can’t perform that action at this time.
0 commit comments