/* 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); } }
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]
Display the bits within a byte
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Display the bits within a byte. using System; public class ShowBits { public static void Main() { int t; byte val; val = 123; for(t=128; t > 0; t = t/2) { if((val & t) != 0) Console.Write("1 "); if((val & t) == 0) Console.Write("0 "); } } }
Use bitwise AND to determine if a number is odd
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use bitwise AND to determine if a number is odd. using System; public class IsOdd { public static void Main() { ushort num; num = 10; if((num & 1) == 1) Console.WriteLine("This won't display."); num = 11; if((num & 1) == 1) Console.WriteLine(num + " is odd."); } }
Use bitwise AND to make a number even
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use bitwise AND to make a number even.
using System;
public class MakeEven {
public static void Main() {
ushort num;
ushort i;
for(i = 1; i <= 10; i++) { num = i; Console.WriteLine("num: " + num); num = (ushort) (num & 0xFFFE); // num & 1111 1110 Console.WriteLine("num after turning off bit zero: " + num + " "); } } } [/csharp]
Shift Operators 2
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_2___Operators_and_Excpressions { public class ShiftOperators { static void Main(string[] args) { uint a = 0; uint b = 0; a = 8 << 3; b = 32 >> 4; } } }
Bit Shift operator
using System;
class CompoundBitOpsApp
{
static void Main(string[] args)
{
for (byte j = 0; j < 8; j++)
{
byte k = 1;
Console.WriteLine(
"1 <<= {0,3} ({1}) = {2,8} ({3,3})",
Convert.ToString(j, 2), j,
Convert.ToString((k <<= j), 2), k);
}
}
}
[/csharp]