/* C# A Beginner's Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295 */ /* Project 5-3 A class that displays the binary representation of a value. */ using System; class ShowBits { public int numbits; public ShowBits(int n) { numbits = n; } public void show(ulong val) { ulong mask = 1; // left-shift a 1 into the proper position mask <<= numbits-1; int spacer = 0; for(; mask != 0; mask >>= 1) { if((val & mask) != 0) Console.Write("1"); else Console.Write("0"); spacer++; if((spacer % 8) == 0) { Console.Write(" "); spacer = 0; } } Console.WriteLine(); } } // Demonstrate ShowBits. public class ShowBitsDemo { public static void Main() { ShowBits b = new ShowBits(8); ShowBits i = new ShowBits(32); ShowBits li = new ShowBits(64); Console.WriteLine("123 in binary: "); b.show(123); Console.WriteLine(" 87987 in binary: "); i.show(87987); Console.WriteLine(" 237658768 in binary: "); li.show(237658768); // you can also show low-order bits of any integer Console.WriteLine(" Low order 8 bits of 87987 in binary: "); b.show(87987); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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 & t) != 0) Console.Write("1 "); if((val & 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 & t) != 0) Console.Write("1 "); if((val & 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 & t) != 0) Console.Write("1 "); if((b & t) == 0) Console.Write("0 "); } Console.WriteLine(); // reverse all bits b = (sbyte) ~b; for(t=128; t > 0; t = t/2) { if((b & t) != 0) Console.Write("1 "); if((b & 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 = 'H'; char ch2 = 'i'; char ch3 = '!'; 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); } }