Category:Jq/bitwise.jq: Difference between revisions

Content deleted Content added
Peak (talk | contribs)
fix bug in flip/1
Peak (talk | contribs)
flip/1 was buggy; fix and flip its input and arg
 
Line 1: Line 1:
NEWS: 2024.04.06 - flip/1

<syntaxhighlight lang="jq">
<syntaxhighlight lang="jq">
module {
module {
"name": "bitwise",
"name": "bitwise",
"description": "bit arrays, bit streams, and integers",
"description": "bit arrays, bit streams, and integers",
"version": "2024.04.05",
"version": "2024.04.06",
"homepage": "https://rosettacode.org/w/index.php?title=Category:Jq/bitwise.jq",
"homepage": "https://rosettacode.org/w/index.php?title=Category:Jq/bitwise.jq",
"license": "MIT",
"license": "MIT",
Line 47: Line 49:
end ;
end ;


# If the input is null, then the bitwise bits of the integer $x are simply flipped;
# If $n is null, then the bitwise bits of the input integer are simply flipped;
# otherwise, the input integer determines the width for padding or truncation
# otherwise, $n, which should be non-negative, determines the width for padding or truncation
def flip($x):
def flip($n):
if . == null then stream_to_integer(flipbits($x|bitwise))
if $n == null then stream_to_integer(flipbits(bitwise))
else stream_to_integer(flipbits( limit(.; $x|bitwise) ) )
else [limit($n; bitwise)] as $bits
| stream_to_integer(flipbits( $bits[], (range(0; $n - ($bits|length)) | 0)))
end;
end;