Set right-adjacent bits: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(Added Quackery.) |
||
Line 1,253: | Line 1,253: | ||
Input b: 010000000000100000000010000000010000000100000010000010000100010010 |
Input b: 010000000000100000000010000000010000000100000010000010000100010010 |
||
Result: 011110000000111100000011110000011110000111100011110011110111111111</pre> |
Result: 011110000000111100000011110000011110000111100011110011110111111111</pre> |
||
=={{header|Quackery}}== |
|||
<code>bin</code> sprinkles a little syntactic sugar by extending the compiler to understand binary numbers. |
|||
<syntaxhighlight lang="Quackery"> [ 2 base put |
|||
nextword dup |
|||
$ '' = if |
|||
[ $ '"bin" needs a number after it.' |
|||
message put |
|||
bail ] |
|||
dup $->n iff |
|||
[ nip swap dip join ] |
|||
else |
|||
[ drop |
|||
char " swap join |
|||
$ '" is not binary.' |
|||
join message put |
|||
bail ] |
|||
base release ] builds bin ( [ $ --> [ $ ) |
|||
[ [] unrot |
|||
times |
|||
[ dup 1 & |
|||
rot join swap |
|||
1 >> ] |
|||
drop |
|||
witheach echo ] is echobin ( n n --> ) |
|||
[ dip dup times |
|||
[ 1 >> tuck | swap ] |
|||
drop ] is setrightbits ( n --> n ) |
|||
say "n = 2; Width e = 4:" |
|||
cr cr |
|||
' [ bin 1000 bin 0100 |
|||
bin 0010 bin 0001 ] |
|||
witheach |
|||
[ say "Input b: " |
|||
dup 4 echobin cr |
|||
say "Result: " |
|||
2 setrightbits |
|||
4 echobin cr cr ] |
|||
4 times |
|||
[ say "n = " i^ echo |
|||
say " Width e = 66:" cr |
|||
say "Input b:" cr |
|||
bin 010000000000100000000010000000010000000100000010000010000100010010 |
|||
dup 66 echobin cr |
|||
say "Result:" cr |
|||
i^ setrightbits |
|||
66 echobin cr cr ]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>n = 2; Width e = 4: |
|||
Input b: 1000 |
|||
Result: 1110 |
|||
Input b: 0100 |
|||
Result: 0111 |
|||
Input b: 0010 |
|||
Result: 0011 |
|||
Input b: 0001 |
|||
Result: 0001 |
|||
n = 0 Width e = 66: |
|||
Input b: |
|||
010000000000100000000010000000010000000100000010000010000100010010 |
|||
Result: |
|||
010000000000100000000010000000010000000100000010000010000100010010 |
|||
n = 1 Width e = 66: |
|||
Input b: |
|||
010000000000100000000010000000010000000100000010000010000100010010 |
|||
Result: |
|||
011000000000110000000011000000011000000110000011000011000110011011 |
|||
n = 2 Width e = 66: |
|||
Input b: |
|||
010000000000100000000010000000010000000100000010000010000100010010 |
|||
Result: |
|||
011100000000111000000011100000011100000111000011100011100111011111 |
|||
n = 3 Width e = 66: |
|||
Input b: |
|||
010000000000100000000010000000010000000100000010000010000100010010 |
|||
Result: |
|||
011110000000111100000011110000011110000111100011110011110111111111 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |