CoffeeScript - Bitwise operators



CoffeeScript supports the following bitwise operators. Assume variable A holds 2 and variable B holds 3, then −

Sr.No Operator and Description Example
1

& (Bitwise AND)

It performs a Boolean AND operation on each bit of its integer arguments.

(A & B) is 2.
2

| (BitWise OR)

It performs a Boolean OR operation on each bit of its integer arguments.

(A | B) is 3.
3

^ (Bitwise XOR)

It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both.

(A ^ B) is 1.
4

~ (Bitwise Not)

It is a unary operator and operates by reversing all the bits in the operand.

(~B) is -4.
5

<< (Left Shift)

It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

(A << 1) is 4.
6

>> (Right Shift)

Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand.

(A >> 1) is 1.

Example

The following example demonstrates the usage of bitwise operators in CoffeeScript. Save this code in a file with name bitwise_example.coffee

a = 2 # Bit presentation 10
b = 3 # Bit presentation 11

console.log "The result of (a & b) is "
result = a & b
console.log result

console.log "The result of (a | b) is "
result = a | b
console.log result

console.log "The result of (a ^ b) is "
result = a ^ b
console.log result

console.log "The result of (~b) is "
result = ~b
console.log result

console.log "The result of (a << b) is "
result = a << b
console.log result

console.log "The result of (a >> b) is "
result = a >> b
console.log result

Open the command prompt and compile the .coffee file as shown below.

c:/> coffee -c bitwise_example.coffee

On compiling, it gives you the following JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, result;
  a = 2;
  b = 3;

  console.log("The result of (a & b) is ");
  result = a & b;
  console.log(result);

  console.log("The result of (a | b) is ");
  result = a | b;
  console.log(result);

  console.log("The result of (a ^ b) is ");
  result = a ^ b;
  console.log(result);

  console.log("The result of (~b) is ");
  result = ~b;
  console.log(result);

  console.log("The result of (a << b) is ");
  result = a << b;
  console.log(result);

  console.log("The result of (a >> b) is ");
  result = a >> b;
  console.log(result);

}).call(this);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:/> coffee bitwise_example.coffee

On executing, the CoffeeScript file produces the following output.

The result of (a & b) is
2
The result of (a | b) is
3
The result of (a ^ b) is
1
The result of (~b) is
-4
The result of (a << b) is
16
The result of (a >> b) is
0
coffeescript_operators_and_aliases.htm
Advertisements