Bit wise, Bit wise shift operators
Notes:
I. C# Bit wise Operators: [Starts at: 00min:48sec]
II. C# Bit wise Shift Operators: [Starts at: 18min:56sec]
I. C# Bit wise Operators: [Starts at: 00min:48sec]
Bitwise operators first convert the given integer operands to binary
Then perform operations on bits (i.e. 0 or 1)
Convert the result back to integer and return
bitwise AND (&),
bitwise inclusive OR (|)
bitwise exclusive OR (^)
bitwise complement (~)
II. C# Bit wise Shift Operators: [Starts at: 18min:56sec]
bitwise left shift (<<)
bitwise right shift (>>)
Example Code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Bitwise operators");
// if both LHS and RHS are 1 then result will be 1
Debug.Log (1 & 0); // 0
// if both LHS and RHS are 0 then result will be 0
Debug.Log (1 | 0); // 1
// if both LHS and RHS are same then result will be 0
Debug.Log (1 ^ 0); // 1
Debug.Log (2 & 2); // 0010 & 0010 = 0010 = 2
Debug.Log (2 | 5); // 0010 | 0101 = 111 = 7
// add 1 and change the sign
Debug.Log (~2); // 2 + 1 = +3 = -3
Debug.Log(~(-2)); // -2 + 1 = -1 = +1
// Bitwise shift operators
Debug.Log (1 << 4); // 1 * pow(2,4) = 16
Debug.Log (16 >> 4); // 16 / pow(2,4) = 1
}
}