
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate.

All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer.

If any operation is not available in your language, note it.




/**
 <doc>
   <h2>bitwise operations</h2>
   <p>Tectonics:
     <br>  nekoc bitwise.neko
     <br>  neko bitwise</p>
 </doc>
*/
 
// Neko is a signed 31 bit integer VM, full 32 bit requires builtins
var int32_new = $loader.loadprim("std@int32_new", 1);
var int32_and = $loader.loadprim("std@int32_and", 2);
var int32_or = $loader.loadprim("std@int32_or", 2);
var int32_xor = $loader.loadprim("std@int32_xor", 2);
var int32_shl = $loader.loadprim("std@int32_shl", 2);
var int32_shr = $loader.loadprim("std@int32_shr", 2);
var int32_ushr = $loader.loadprim("std@int32_ushr", 2);
var int32_complement = $loader.loadprim("std@int32_complement", 1);
 
// Function to show bitwise operations on a,b
var bitwise = function(a, b) {
  var ia = int32_new(a);
  var ib = int32_new(b);
 
  $print("Neko 32 bit integer library\n");
  $print("a AND b: ", a, " ", b, " ", int32_and(ia, ib), "\n");
  $print("a OR b:  ", a, " ", b, " ", int32_or(ia, ib), "\n");
  $print("a XOR b: ", a, " ", b, " ", int32_xor(ia, ib), "\n");
  $print("ones complement a:   ", a, " ", int32_complement(ia), "\n");
  $print("a SHL b: ", a, " ", b, " ", int32_shl(ia, ib), "\n");
  $print("a SHR b: ", a, " ", b, " ", int32_shr(ia, ib), "\n");
  $print("a USHR b: ", a, " ", b, " ", int32_ushr(ia, ib), "\n");
  $print("a ROL b: is not directly supported in Neko Int32\n");
  $print("a ROR b: is not directly supported in Neko Int32\n");
 
  $print("\nNormal Neko 31 bit signed integers\n");
  a = $int(a);
  b = $int(b);
  $print("a AND b: ", a, " ", b, " ", a & b, "\n");
  $print("a OR  b: ", a, " ", b, " ", a | b, "\n");
  $print("a XOR b: ", a, " ", b, " ", a ^ b, "\n");
  $print("NOT a: is not directly supported in Neko syntax\n");
  $print("a SHL b: ", a, " ", b, " ", a << b, "\n");
  $print("a SHR b: ", a, " ", b, " ", a >> b, "\n");
  $print("a USHR b: ", a, " ", b, " ", a >>> b, "\n");
  $print("a ROL b: is not directly supported in Neko syntax\n");
  $print("a ROR b: is not directly supported in Neko syntax\n");
}
 
// Pass command line arguments to the demo function
// initially as float, to ensure no internal bit truncation
var a = $float($loader.args[0]);
var b = $float($loader.args[1]);
if a == null a = 0;
if b == null b = 0;
 
bitwise(a,b);