Csharp/C Sharp/Data Types/Binary Bit

Материал из .Net Framework эксперт
Версия от 11:46, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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);
   }
}


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);
   }
}


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);
        }
}