Csharp/C Sharp/Language Basics/Operator bitwise — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:39, 26 мая 2010
Содержание
- 1 A class that displays the binary representation of a value
- 2 Bit and, or, xor, not operator
- 3 Bit move operator
- 4 Bit operator
- 5 Bit Operators: move
- 6 Bit Shift operator
- 7 Bitwise operation
- 8 Bitwise Operators 2
- 9 Demonstrate the bitwise NOT
- 10 Demonstrate the shift operators
- 11 Display the bits within a byte
- 12 Shift Operators 2
- 13 Show bits
- 14 Use bitwise AND to determine if a number is odd
- 15 Use bitwise AND to make a number even
- 16 Use bitwise OR to make a number odd
- 17 Use the shift operators to multiply and divide by 2
- 18 Use XOR to encode and decode a message
A class that displays the binary representation of a value
/*
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("\n87987 in binary: ");
i.show(87987);
Console.WriteLine("\n237658768 in binary: ");
li.show(237658768);
// you can also show low-order bits of any integer
Console.WriteLine("\nLow order 8 bits of 87987 in binary: ");
b.show(87987);
}
}
Bit and, or, xor, not operator
using System;
class LogOpsApp
{
public static void display(byte left, byte right, byte ans, string op) {
string Lstr = null;
string Rstr = null;
string Astr = null;
Lstr = Convert.ToString(left, 2);
if (0 != right)
Rstr = Convert.ToString(right, 2);
else
Rstr = "--------";
Astr = Convert.ToString(ans, 2);
Console.WriteLine("\t{0,8}\n{1}\t{2,8}\n\t{3,8}\n",Lstr, op, Rstr, Astr);
}
static void Main(string[] args)
{
byte a, b, c, d, e, f, g;
a = 255;
b = 132;
c = 85;
byte OneOperand = 0;
d = (byte)(a & b);
display (a, b, d, "&");
e = (byte)(d | c);
display (d, c, e, "|");
f = (byte)(e ^ a);
display (e, a, f, "^");
g = (byte)~f;
display (f, OneOperand, g, "~");
}
}
Bit move operator
using System;
class Operators {
static void Main() {
int a = 256, b = 128, c, d;
c = a >> 1;
d = b << 1;
Console.WriteLine("{0}", c); //128
Console.WriteLine("{0}", d); //256
}
}
Bit operator
using System;
class Operators {
static void Main() {
int a = 10, b = 15, c = 20, d, e, h, i;
bool f, g, j = (a == b), k;
d = (a & b);
e = (a | b);
f = (j && (b == c));
g = (j || (b == c));
h = (a ^ b);
i = ~b;
k = !j;
Console.WriteLine("{0}", d); //10
Console.WriteLine("{0}", e); //15
Console.WriteLine("{0}", f); //False
Console.WriteLine("{0}", g); //False
Console.WriteLine("{0}", h); //5
Console.WriteLine("{0}", i); //-16
Console.WriteLine("{0}", j); //False
Console.WriteLine("{0}", k); //True
}
}
Bit Operators: move
using System;
class MainClass
{
static void Main(string[] args)
{
int i = 1;
for (int j = 0; j < 8; j++)
{
Console.WriteLine(
"{0} << {1} = {2}", i, j, i << j);
}
}
}
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);
}
}
}
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);
}
}
}
}
Bitwise 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 BitwiseOperators
{
static void Main(string[] args)
{
long MyBit = 0x1;
long MyBitResult = 0;
MyBitResult = MyBit & 0x1;
MyBitResult = MyBit | 0x2;
MyBitResult = MyBit ^ 0x4;
}
}
}
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 ");
}
}
}
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
}
}
}
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 ");
}
}
}
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;
}
}
}
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 ("\r\ny = {0} = ", y);
ShowBits (y);
ushort result = (ushort) (x & y);
Console.Write ("\r\nx & y = ");
ShowBits (result);
Console.WriteLine (" = " + result);
Console.Write ("\r\nx = {0} = ", x);
ShowBits (x);
Console.Write ("\r\ny = {0} = ", y);
ShowBits (y);
result = (ushort) (x | y);
Console.Write ("\r\nx | y = ");
ShowBits (result);
Console.WriteLine (" = " + result);
Console.Write ("\r\nx = {0} = ", x);
ShowBits (x);
Console.Write ("\r\ny = {0} = ", y);
ShowBits (y);
result = (ushort) (x ^ y);
Console.Write ("\r\nx ^ 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 (" ");
}
}
}
}
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 + "\n");
}
}
}
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 + "\n");
}
}
}
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);
}
}
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);
}
}