Skip to content

Commit deb367a

Browse files
committed
Adds examples and expands explanation for +^. Closes #3177
1 parent b7c4120 commit deb367a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

doc/Language/operators.pod6

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,26 @@ arguments.
11471147
11481148
multi sub prefix:<+^>(Any --> Int:D)
11491149
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.
11531170
11541171
=head2 prefix C«~^»
11551172

0 commit comments

Comments
 (0)