lshift (object shiftCount -- shiftedValue)
Inputs
Outputs
shiftedValue: Left-shift result in the same schema.
lshift consumes two Whole objects. object provides the bits to shift and the schema of the result. shiftCount provides the shift amount and may use a different Whole schema.
The bits of object move to the left. Zero bits are inserted at the low end and bits shifted past the high end are discarded.
The result always keeps the same schema as object.
Behavior
- For Nat and Int, zero bits are inserted at the low end.
- If
shiftCountuses an Int schema, its bit pattern is first reinterpreted as the matching Nat width. Non-negative Int shift counts therefore behave the same as equal-valued Nat shift counts. - If
shiftCount, after that reinterpretation, is at least the bit width ofobject, the behavior of the shift is not defined; this includes every negative Int count. Neither compile time nor run time checks the count. - For Int, the shifted bit pattern is interpreted again as the same signed schema. In Int32,
1 31 lshifttherefore produces-2147483648. - If both inputs are known at compile time, the result is computed at compile time.
- If
objectis known zero, the result is known zero even whenshiftCountis unknown; an excessive count remains not defined. - Otherwise, an unknown input produces an unknown result of the same schema as
object.
Example
{} () {} [
"-- nat32 --" printCompilerMessage
3n32 2 lshift printStack _:;
3n32 1i8 lshift printStack _:;
"-- nat8 dropped bits --" printCompilerMessage
0x80n8 1 lshift printStack _:;
"-- zero and unknown shift --" printCompilerMessage
unknownShift: 0n32 dynamic;
0n32 unknownShift lshift printStack _:;
"-- unknown value and zero shiftCount --" printCompilerMessage
unknownNat: 0n32 dynamic;
unknownNat 0 lshift printStack _:;
"-- int32 --" printCompilerMessage
5 1 lshift printStack _:;
1 31 lshift printStack _:;
] "main" exportFunction
Expected Output During Compilation
-- nat32 --
12n32
6n32
-- nat8 dropped bits --
0n8
-- zero and unknown shift --
0n32
-- unknown value and zero shiftCount --
Nat32
-- int32 --
10
-2147483648
See also
andbuiltin(leftOperand rightOperand -- combinedValue)Combines two Cond or Whole objects with bitwise or logical AND semantics.orbuiltin(leftOperand rightOperand -- combinedValue)Combines two Cond or Whole objects with bitwise or logical OR semantics.rshiftbuiltin(object shiftCount -- shiftedValue)Performs a bitwise right shift on a Whole object and keeps the same schema.xorbuiltin(leftOperand rightOperand -- combinedValue)Combines two Cond or Whole objects with logical exclusive-OR or bitwise XOR semantics.~builtin(object -- invertedValue)Performs logical NOT for Cond or bitwise complement for a Whole object.