Csharp/CSharp Tutorial/Data Type/Bits

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bitwise Operators

<source lang="csharp">using System; class MainClass {

 static void Main(string[] args)
 {
   long MyBit = 0x1;
   long MyBitResult = 0;
   MyBitResult = MyBit & 0x1;
   Console.WriteLine(MyBitResult);
   
   MyBitResult = MyBit | 0x2;
   Console.WriteLine(MyBitResult);
   
   MyBitResult = MyBit ^ 0x4;
   Console.WriteLine(MyBitResult);
 }

}</source>

1
3
5

Display the bits within a byte.

<source lang="csharp">using System;

class Example {

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

}</source>

0 1 1 1 1 0 1 1