New bitwise operators

These blocks, in the 7.1.3 Bitwise operators library:
untitled script pic (71)

Two comments on them:

  1. As cymplecy said here:
  2. Where's unsigned left shift (<<<)?

Right, I'm agitating for BITWISE AND etc.

There are two right shifts because one of them shifts in zeros from the left and the other shifts in zeros or ones depending on the original leftmost bit. (If you want to know why that makes sense, look up two's complement. But there's only one kind of left shift, because there's never a reason to shift in ones from the right.

I'm guessing some language has both <<< and << that do the same thing. But if we use words in the names, there'll be LEFT SHIFT, not ARITHMETIC LEFT SHIFT and LOGICAL LEFT SHIFT.

So one of them turns 110100010 into 011010001, but the other turns it into 111010001?

Yes. See if the two's complement article explains it well enough.

I will later. Probably in a few days.

It isn't used very much, and you can recreate it with a few other bitwise blocks. It also depends on the size of the data.
For bytes, x <<< y = ((x << y) ∧ 255) ∨ (x >>> (8 - y)).
For other bitstrings of length n, x <<< y = ((x << y) ∧ 2ⁿ-1) ∨ (x >>> (n - y)).

Huh. I'll have to see how that works later.

Are you saying it means rotate?

Yes. It shifts the bits left, but the larger bits wrap around so they don't overflow. It doesn't really make sense with infinite precision integers or floats.

Well then its name is "rotate," not "unsigned left shift." :~)

So, I've made modified library blocks to use names for NOT/AND/OR and XOR

And added helps in for them



<blocks app="Snap! 7," version="2"><block-definition s="NOT %&apos;a&apos;" type="reporter" category="operators"><comment w="90" collapsed="false">Swap each indeividual bit of the value from a 1 to a 0 or 0 to a 1</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_not(a)</l><list><block var="a"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; AND %&apos;b&apos;" type="reporter" category="operators"><comment w="146" collapsed="false">Compare each indiviaul bit of the two values and logically AND them together</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_and(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; OR %&apos;b&apos;" type="reporter" category="operators"><comment w="211" collapsed="false">compare each indivual bit of of the two values and apply logical OR operation on them</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_or(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; XOR %&apos;b&apos;" type="reporter" category="operators"><comment w="144" collapsed="false">Exclusive or each individual bit of of the two values</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_xor(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; &lt;&lt; %&apos;b&apos;" type="reporter" category="operators"><comment w="90" collapsed="false">Shift all the bits of the first value to the left by the second value amount.&#xD;&#xD;Note: Shifting left 1 place is the same as multiplying a number by 2,</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_left_shift(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; &gt;&gt; %&apos;b&apos;" type="reporter" category="operators"><comment w="234" collapsed="false">Shift all the bits of the first value to the right by the second value amount.&#xD;&#xD;Note: Shifting right 1 place is the same as an integer division by 2,.</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_right_shift(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition><block-definition s="%&apos;a&apos; &gt;&gt;&gt; %&apos;b&apos;" type="reporter" category="operators"><comment w="163" collapsed="false">Shift all the bits of the first value to the left by the second value amount but leave the leftmost bit alone.&#xD;&#xD;This is used to deal with two-complements, negative number represtations</comment><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportApplyExtension"><l>bit_unsigned_right_shift(a, b)</l><list><block var="a"/><block var="b"/></list></block></block></script></block-definition></blocks>

Why names for the bitwise operators but not names for the shift operators? Especially the right shifts; I don't know which is which. LEFT SHIFT, LOGICAL RIGHT SHIFT, ARITHMETIC RIGHT SHIFT.

I thought that the arrows are pretty universally understood (to people used to bit shift operators) and that the length of any labelling would make the blocks look unwieldy (AND/OR/NOT/XOR are very succinct).

But it's a discussion point - but we really need Bernat and Jens to join in the discussion :slight_smile:

I point out that there were a lot of questions on the forum about what an arithmetic shift means and why there isn't an arithmetic left shift.

Also, I don't think that just capitalizing the names is enough to clarify the difference between Boolean AND and bitwise AND. Especially when people like me use uppercase to mean program text. :~)

I'm all for a (even much) more explicit labelling of these extension blocks. But at the moment I've got other things on my plate that have priority, and we really only added those extensions to support some hardware tricks that a bunch of teachers are piloting right now. This isn't where any major development happens at the moment :slight_smile:

Bernat is also in his last week of work before going on a multi-month parental leave, so there are also more urgent projects for him to tie up. As always, grant us some time to make things nice, in the meantime you're invited to already play with them.

If it helps, in Logo we called these BITAND, BITOR, BITNOT, LSHIFT, ASHIFT. We didn't have separate names for left and right shift; LSHIFT and ASHIFT shift left for positive second inputs, right for negative.