Wednesday, February 15, 2012

C# Bitwise Shift Operators

C# Bitwise Shift Operators
The bitwise shift operators in the C# language to an integral type provides a way to change the bit representation of a type so that the bits are shifted right or shifted left a certain number of positions. The C# language enables bitwise shifting by offering the right shift (>>) and left shift (<<) operators directly in the language. The term "bitwise operator" indicates an operator that receives one or two operands and is meant to change the bit representation of an integer.
This post is all about the C# Bitwise shift operators, using these bitwise operator we will first revise some basics first.

Integral Types
Type
Size (in bits)
Range
sbyte
8
-128 to 127
byte
8
0 to 255
short
16
-32768 to 32767
ushort
16
0 to 65535
int
32
-2147483648 to 2147483647
uint
32
0 to 4294967295
long
64
-9223372036854775808 to 9223372036854775807
ulong
64
0 to 18446744073709551615
char
16
0 to 65535

Integral types are well suited for those operations involving whole number calculations. The char type is the exception, representing a single Unicode character. As you can see from the table above, you have a wide range of options to choose from, depending on your requirements.

Floating Point and Decimal Types
A C# floating point type is either a float or double. They are used any time you need to represent a real number. Decimal types should be used when representing financial or money values.

Type
Size (in bits)
precision
Range
float
32
7 digits
1.5 x 10-45 to 3.4 x 1038
double
64
15-16 digits
5.0 x 10-324 to 1.7 x 10308
decimal
128
28-29 decimal places
1.0 x 10-28 to 7.9 x 1028

Floating point types are used when you need to perform operations requiring fractional representations. However, for financial calculations, the decimal type is the best choice because you can avoid rounding errors.

C# Operators
The following table describes the allowable operators, their precedence, and associativity.


Operator Categories
Operator(s)
Associativity
Primary
x.y  f(x)  a[x]  x++  x--  new  typeof  default  checked  unchecked delegate
left
Unary
+  -  !  ~  ++x  --x  (T)x
right
Multiplicative
*  /  %
left
Additive
+  -
left
Shift
<<  >>
left
Relational
<  >  <=  >=  is as
left
Equality
==  !=
right
Logical AND
&
left
Logical XOR
^
left
Logical OR
|
left
Conditional AND
&&
left
Conditional OR
||
left
Null Coalescing
??
left
Ternary
?:
right
Assignment
=  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=  =>
right
Left associativity means that operations are evaluated from left to right.
Right associativity mean all operations occur from right to left, such as assignment operators where everything to the right is evaluated before the result is placed into the variable on the left.
Most operators are either unary or binary. Unary operators form expressions on a single variable, but binary operators form expressions with two variables.

Shift Left Operator
The shift operators allow programmers to adjust an integer by shifting all of its bits to the left or the right. The following diagram shows the affect of shifting a value to the left by one digit. 
  00001000  =  8 
After Applying SHIFT LEFT 
 00010000  =  16
As you can see, each bit is moved to the left and the lowest order bit becomes zero. As this is a binary operation, the effect of shifting to the left is to double the value. In fact, this method is often used to perform some multiplication as it can be faster than using arithmetic multiplication.
In C#, the shift left operator is represented as two less than signs (<<). The operator can be used to shift the binary number by one or more digits. The number of digits is specified after the operator, as in the following code:


        /// Claculates multiples of two 2
        static long MultipleOfTwo(long value, int  noTimesShift)
        {
            long result = 0;
             result = value << noTimesShift-1;           
            return result;
        }
Shift Right Operator
The shift right operator provides the reverse of shift left, moving each bit to the right by a number of digits. C# uses two greater than signs (>>) for the operator. 
  00010000  =  16 
After Applying SHIFT RIGHT 
  00001000  =  8 


        /// Divides a value by Two 2
        static long DivideByTwo(long value, int noTimesShift)
        {
            long result = 0;
            result = value >> noTimesShift - 1;
            return result;
        }
       
        public static string ToBinary(long Decimal)
        {
            // Declare a few variables we're going to need
            long BinaryHolder;
            char[] BinaryArray;
            string BinaryResult = "";

            while (Decimal > 0)
            {
                BinaryHolder = Decimal % 2;
                BinaryResult += BinaryHolder;
                Decimal = Decimal / 2;
            }

            BinaryArray = BinaryResult.ToCharArray();
            Array.Reverse(BinaryArray);
            BinaryResult = new string(BinaryArray);

            return BinaryResult;
        }

        static void Main(string[] args)
        {
            //For LeftShift Operator
            for (int i = 1; i <= 16; i++)
            {
                long multiple = MultipleOfTwo(2, i);             
                Console.WriteLine("{0}^{1}={2} (B{3} --> B{4})", 2, i, multiple,ToBinary(i), ToBinary(multiple));
            }

            //For RightShift Operator
            for (int i = 16; i >= 1; i--)
            {
                long multiple = MultipleOfTwo(2, i);
                long div = DivideByTwo(multiple, 2);            
                Console.WriteLine("{0}/{1} = {2} (B{3}) --> (B{4})", multiple, 2, div, ToBinary(multiple), ToBinary(div));
            }
            Console.ReadLine();
        }


 OutPut: 
 2^1=2 (B1 --> B10)
 2^2=4 (B10 --> B100) 
 2^3=8 (B11 --> B1000) 
 2^4=16 (B100 --> B10000) 
 2^5=32 (B101 --> B100000) 
 2^6=64 (B110 --> B1000000) 
 2^7=128 (B111 --> B10000000) 
 2^8=256 (B1000 --> B100000000)
 2^9=512 (B1001 --> B1000000000) 
 2^10=1024 (B1010 --> B10000000000) 
 2^11=2048 (B1011 --> B100000000000)
 2^12=4096 (B1100 --> B1000000000000) 
 2^13=8192 (B1101 --> B10000000000000)
 2^14=16384 (B1110 --> B100000000000000) 
 2^15=32768 (B1111 --> B1000000000000000)
 2^16=65536 (B10000 --> B10000000000000000) 
 
 65536/2 = 32768 (B10000000000000000) --> (B1000000000000000) 
 32768/2 = 16384 (B1000000000000000) --> (B100000000000000)
 16384/2 = 8192 (B100000000000000) --> (B10000000000000)
 8192/2 = 4096 (B10000000000000) --> (B1000000000000) 
 4096/2 = 2048 (B1000000000000) --> (B100000000000) 
 2048/2 = 1024 (B100000000000) --> (B10000000000) 
 1024/2 = 512 (B10000000000) --> (B1000000000) 
 512/2 = 256 (B1000000000) --> (B100000000)
 256/2 = 128 (B100000000) --> (B10000000) 
 128/2 = 64 (B10000000) --> (B1000000) 
 64/2 = 32 (B1000000) --> (B100000) 
 32/2 = 16 (B100000) --> (B10000) 
 16/2 = 8 (B10000) --> (B1000)
 8/2 = 4 (B1000) --> (B100) 
 4/2 = 2 (B100) --> (B10)
 2/2 = 1 (B10) --> (B1)
 
Overflow Bits
When using either shift function, one bit, called the overflow bit, will be shifted outside of the binary value. The value of this digit is lost during the operation and cannot be recovered. Should the value of the bit be important then it should be tested before the shifting using a logical bitwise operator. The loss overflow data is important if the shift operations are being used for multiplication or division to ensure the correct result:
            uint val = 35;
            Console.WriteLine(val>>1);  // output 17

Signed Integers
As indicated in a previous article, signed integers use the highest order bit to determine if a value is positive or negative and that the remaining bits use two's complement notation for negative values The highest order bit would normally be considered as the overflow bit for a shift left operation. To allow for this, C# ignores the bit for signed data types and shifts negative values accordingly. Thus shifting works for positive and negative values.
            int value = -300;
            Console.WriteLine(value >> 1);  //output -150
 
Write a function to calculate A^B?

        /// Calculates A^B i.e. in this function bases^power
        static double PowerOfNumber(double bases, double power)
        {
            double result = 1;
            for(int i=0;i<=power-1;i++)
                result *= bases;
            return result;
        }

No comments:

Post a Comment