Show bits


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsBitwise
{
    using System;
    public class Bitwise123
    {
        static public void Main ()
        {
            ushort x = 15542;
            ushort y = 21845;
            Console.Write ("x = {0} = ", x);
            ShowBits (x);
            Console.Write ("
y = {0} = ", y);
            ShowBits (y);
            ushort result = (ushort) (x & y);
            Console.Write ("
x & y     = ");
            ShowBits (result);
            Console.WriteLine (" = " + result);

            Console.Write ("
x = {0} = ", x);
            ShowBits (x);
            Console.Write ("
y = {0} = ", y);
            ShowBits (y);
            result = (ushort) (x | y);
            Console.Write ("
x | y     = ");
            ShowBits (result);
            Console.WriteLine (" = " + result);

            Console.Write ("
x = {0} = ", x);
            ShowBits (x);
            Console.Write ("
y = {0} = ", y);
            ShowBits (y);
            result = (ushort) (x ^ y);
            Console.Write ("
x ^ y     = ");
            ShowBits (result);
            Console.WriteLine (" = " + result);
        }

        static void ShowBits (ushort x)
        {
            int size;
            unsafe
            {
                size = sizeof (short) * 8;
            }
            for (int i = size - 1; i >= 0; --i)
            {
                Console.Write ((x >> i) & 1);
                if ((i % 4) == 0)
                    Console.Write (' ');
            }
        }
    }
}

           
          


Bitwise operation

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsBitwise
{
using System;
public class Bitwise345
{
static public void Main ()
{
char ch = 'a';
char toggle = (char) 0x20;
for (int x = 0; x < 4; ++x) { Console.WriteLine ("In iteration {0}, ch = {1}", x + 1, (char) ch); ch = (char) (ch ^ toggle); } } } } [/csharp]

Use the shift operators to multiply and divide by 2

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use the shift operators to multiply and divide by 2.
using System;

public class MultDiv {
public static void Main() {
int n;

n = 10;

Console.WriteLine(“Value of n: ” + n);

// multiply by 2
n = n << 1; Console.WriteLine("Value of n after n = n * 2: " + n); // multiply by 4 n = n << 2; Console.WriteLine("Value of n after n = n * 4: " + n); // divide by 2 n = n >> 1;
Console.WriteLine(“Value of n after n = n / 2: ” + n);

// divide by 4
n = n >> 2;
Console.WriteLine(“Value of n after n = n / 4: ” + n);
Console.WriteLine();

// reset n
n = 10;
Console.WriteLine(“Value of n: ” + n);

// multiply by 2, 30 times
n = n << 30; // data is lost Console.WriteLine("Value of n after left-shifting 30 places: " + n); } } [/csharp]

Demonstrate the shift operators


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the shift << and >> operators. 
using System; 
 
public class ShiftDemo { 
  public static void Main() { 
    int val = 1; 
    int t; 
    int i; 
 
    for(i = 0; i < 8; i++) {  
      for(t=128; t > 0; t = t/2) { 
        if((val &amp; t) != 0) Console.Write("1 ");  
        if((val &amp; t) == 0) Console.Write("0 ");  
      } 
      Console.WriteLine(); 
      val = val << 1; // left shift 
    } 
    Console.WriteLine(); 
 
    val = 128; 
    for(i = 0; i < 8; i++) {  
      for(t=128; t > 0; t = t/2) { 
        if((val &amp; t) != 0) Console.Write("1 ");  
        if((val &amp; t) == 0) Console.Write("0 ");  
      } 
      Console.WriteLine(); 
      val = val >> 1; // right shift 
    } 
  } 
}

           
          


Demonstrate the bitwise NOT


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the bitwise NOT. 
using System; 
 
public class NotDemo { 
  public static void Main() { 
    sbyte b = -34; 
    int t; 
 
    for(t=128; t > 0; t = t/2) { 
      if((b &amp; t) != 0) Console.Write("1 ");  
      if((b &amp; t) == 0) Console.Write("0 ");  
    } 
    Console.WriteLine(); 
 
    // reverse all bits 
    b = (sbyte) ~b; 
 
    for(t=128; t > 0; t = t/2) { 
      if((b &amp; t) != 0) Console.Write("1 ");  
      if((b &amp; t) == 0) Console.Write("0 ");  
    } 
  } 
} 


           
          


Use XOR to encode and decode a message


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use XOR to encode and decode a message. 
 
using System; 
 
public class Encode {  
  public static void Main() { 
    char ch1 = &#039;H&#039;; 
    char ch2 = &#039;i&#039;; 
    char ch3 = &#039;!&#039;; 
 
    int key = 88; 
 
    Console.WriteLine("Original message: " + 
                      ch1 + ch2 + ch3); 
 
    // encode the message 
    ch1 = (char) (ch1 ^ key); 
    ch2 = (char) (ch2 ^ key); 
    ch3 = (char) (ch3 ^ key); 
 
    Console.WriteLine("Encoded message: " +  
                      ch1 + ch2 + ch3); 
 
    // decode the message 
    ch1 = (char) (ch1 ^ key); 
    ch2 = (char) (ch2 ^ key); 
    ch3 = (char) (ch3 ^ key); 
    
    Console.WriteLine("Decoded message: " + 
                      ch1 + ch2 + ch3); 
  } 
}

           
          


Use bitwise OR to make a number odd

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use bitwise OR to make a number odd.
using System;

public class MakeOdd {
public static void Main() {
ushort num;
ushort i;

for(i = 1; i <= 10; i++) { num = i; Console.WriteLine("num: " + num); num = (ushort) (num | 1); // num | 0000 0001 Console.WriteLine("num after turning on bit zero: " + num + " "); } } } [/csharp]