using System;
class MainClass
{
static void Main()
{
int a = 0x0c;
a ^= 0x06;
Console.WriteLine("0x{0:x8}", a);
bool b = true;
b ^= false;
Console.WriteLine(b);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Binary Network Byte Order
using System;
using System.Net;
using System.Text;
public class BinaryNetworkByteOrder
{
public static void Main()
{
short test1 = 45;
int test2 = 314159;
long test3 = -123456789033452;
byte[] data = new byte[1024];
string output;
data = BitConverter.GetBytes(test1);
output = BitConverter.ToString(data);
Console.WriteLine("test1 = {0}, string = {1}", test1, output);
data = BitConverter.GetBytes(test2);
output = BitConverter.ToString(data);
Console.WriteLine("test2 = {0}, string = {1}", test2, output);
data = BitConverter.GetBytes(test3);
output = BitConverter.ToString(data);
Console.WriteLine("test3 = {0}, string = {1}", test3, output);
short test1b = IPAddress.HostToNetworkOrder(test1);
data = BitConverter.GetBytes(test1b);
output = BitConverter.ToString(data);
Console.WriteLine("test1 = {0}, nbo = {1}", test1b, output);
int test2b = IPAddress.HostToNetworkOrder(test2);
data = BitConverter.GetBytes(test2b);
output = BitConverter.ToString(data);
Console.WriteLine("test2 = {0}, nbo = {1}", test2b, output);
long test3b = IPAddress.HostToNetworkOrder(test3);
data = BitConverter.GetBytes(test3b);
output = BitConverter.ToString(data);
Console.WriteLine("test3 = {0}, nbo = {1}", test3b, output);
}
}
Binary Data Test
using System;
using System.Net;
using System.Text;
public class BinaryDataTest
{
public static void Main()
{
int test1 = 45;
double test2 = 3.14159;
int test3 = -1234567890;
bool test4 = false;
byte[] data = new byte[1024];
string output;
data = BitConverter.GetBytes(test1);
output = BitConverter.ToString(data);
Console.WriteLine("test1 = {0}, string = {1}", test1, output);
data = BitConverter.GetBytes(test2);
output = BitConverter.ToString(data);
Console.WriteLine("test2 = {0}, string = {1}", test2, output);
data = BitConverter.GetBytes(test3);
output = BitConverter.ToString(data);
Console.WriteLine("test3 = {0}, string = {1}", test3, output);
data = BitConverter.GetBytes(test4);
output = BitConverter.ToString(data);
Console.WriteLine("test4 = {0}, string = {1}", test4, output);
}
}
Obtaining the Most Significant or Least Significant Bits of a Number
using System;
using System.Data;
class Class1{
static void Main(string[] args){
int number = 25;
short num = 25;
Console.WriteLine(GetMSB(number));
Console.WriteLine(GetConvertMSB(number));
Console.WriteLine(GetLSB(number));
Console.WriteLine(GetConvertLSB(number));
Console.WriteLine(GetMSB(num));
Console.WriteLine(GetLSB(num));
}
public static int GetMSB(int value)
{
return (int)(value & 0xFFFF0000);
}
public static int GetConvertMSB(int value)
{
return (value & Convert.ToInt32("11111111111111110000000000000000", 2));
}
public static int GetLSB(int intValue)
{
return (intValue & 0x0000FFFF);
}
public static int GetConvertLSB(int intValue)
{
return (intValue & Convert.ToInt32("11111111111111110000000000000000", 2));
}
public static int GetMSB(short intValue)
{
return (intValue & 0xFF00);
}
public static int GetLSB(short intValue)
{
return (intValue & 0x00FF);
}
}
Using the Bitwise Complement Operators with Various Data Types
using System;
using System.Data;
class Class1{
static void Main(string[] args){
uint x = 0x01001001;
uint XComp = ~x;
Console.WriteLine("~x = " + ~x);
sbyte B1 = sbyte.MinValue;
sbyte B2 = sbyte.MaxValue;
Console.WriteLine("B1|B2 = " + (((byte)B1|(byte)B2)));
ushort x2 = 0x00000001; // Problem
Console.WriteLine("~x2 = " + ~x2);
byte y = 1; // Problem
//byte B = ~y;
Console.WriteLine("~y = " + ~y);
char x3 = (char)1; // Problem
Console.WriteLine("~x3 = " + ~x3);
sbyte x5 = 1;
Console.WriteLine("~x5 = " + ~x5);
uint IntResult = (uint)~x;
Console.WriteLine("IntResult = " + IntResult);
byte ByteResult = (byte)~y;
Console.WriteLine("ByteResult = " + ByteResult);
}
}
.NET Frameworks Overview:Custom Object Formatting
using System;
class Employee: IFormattable
{
public Employee(int id, string firstName, string lastName)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public string ToString (string format, IFormatProvider fp)
{
if ((format != null) && (format.Equals("F")))
return(String.Format("{0}: {1}, {2}",
id, lastName, firstName));
else
return(id.ToString(format, fp));
}
int id;
string firstName;
string lastName;
}
public class CustomObjectFormatting
{
public static void Main()
{
Employee fred = new Employee(123, "AAA", "BBB");
Console.WriteLine("No format: {0}", fred);
Console.WriteLine("Full format: {0:F}", fred);
}
}
Overriding the ToString() Method
using System;
public class Name {
public string firstName;
public string lastName;
public Name(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void Display() {
Console.WriteLine("firstName = " + firstName);
Console.WriteLine("lastName = " + lastName);
}
// override the ToString() method
public override string ToString() {
return firstName + " " + lastName;
}
}
class Test{
public static void Main() {
Name myName = new Name("T", "M");
Name myOtherName = new Name("P", "B");
// call the ToString() method for the Name objects
Console.WriteLine("myName.ToString() = " + myName.ToString());
Console.WriteLine("myOtherName.ToString() = " + myOtherName.ToString());
}
}