2009-11-06 9 views
5

Esiste un tipo di dati incorporato Gray code in qualsiasi parte del framework .NET? O utilità di conversione tra Gray e binario? Potrei farlo da solo, ma se la ruota è già stata inventata ...Codice Gray in. NET

+0

Grazie per il collegamento wikipedia. Non ho mai sentito parlare di codici grigi e questo ha reso alcune letture interessanti. –

risposta

12

Utilizzare this trick.

/* 
     The purpose of this function is to convert an unsigned 
     binary number to reflected binary Gray code. 
*/ 
unsigned short binaryToGray(unsigned short num) 
{ 
     return (num>>1)^num; 
} 

Un trucco difficile: fino a 2 bit^n, è possibile convertire Gray binario eseguendo (2^n) - 1 binario a conversioni grigi. Tutto ciò di cui hai bisogno è la funzione in alto e un ciclo 'for'.

/* 
     The purpose of this function is to convert a reflected binary 
     Gray code number to a binary number. 
*/ 
unsigned short grayToBinary(unsigned short num) 
{ 
     unsigned short temp = num^(num>>8); 
     temp ^= (temp>>4); 
     temp ^= (temp>>2); 
     temp ^= (temp>>1); 
     return temp; 
} 
+0

Grazie, Jeff! – Artelius

0

Non c'è niente integrato fino al codice Gray in. NET.

3

Ecco un'implementazione C# che presuppone si desidera solo fare questo su non negativi interi a 32 bit:

static uint BinaryToGray(uint num) 
{ 
    return (num>>1)^num; 
} 

Ti potrebbe piacere anche leggere this blog post che fornisce i metodi per le conversioni in entrambe indicazioni, anche se l'autore ha scelto di rappresentare il codice come una matrice di int contenente uno o zero in ciascuna posizione. Personalmente penserei che una BitArray potrebbe essere una scelta migliore.

1

Forse questa raccolta di metodi è utile

  • sulla base di BitArray
  • entrambe le direzioni
  • int o solo n Bits

semplicemente divertiti.

public static class GrayCode 
{ 
    public static byte BinaryToByte(BitArray binary) 
    { 
     if (binary.Length > 8) 
      throw new ArgumentException("bitarray too long for byte"); 

     var array = new byte[1]; 
     binary.CopyTo(array, 0); 
     return array[0]; 
    } 

    public static int BinaryToInt(BitArray binary) 
    { 
     if (binary.Length > 32) 
      throw new ArgumentException("bitarray too long for int"); 

     var array = new int[1]; 
     binary.CopyTo(array, 0); 
     return array[0]; 
    } 

    public static BitArray BinaryToGray(BitArray binary) 
    { 
     var len = binary.Length; 
     var gray = new BitArray(len); 
     gray[len - 1] = binary[len - 1]; // copy high-order bit 
     for (var i = len - 2; i >= 0; --i) 
     { 
      // remaining bits 
      gray[i] = binary[i]^binary[i + 1]; 
     } 
     return gray; 
    } 

    public static BitArray GrayToBinary(BitArray gray) 
    { 
     var len = gray.Length; 
     var binary = new BitArray(len); 
     binary[len - 1] = gray[len - 1]; // copy high-order bit 
     for (var i = len - 2; i >= 0; --i) 
     { 
      // remaining bits 
      binary[i] = !gray[i]^!binary[i + 1]; 
     } 
     return binary; 
    } 

    public static BitArray ByteToGray(byte value) 
    { 
     var bits = new BitArray(new[] { value }); 
     return BinaryToGray(bits); 
    } 

    public static BitArray IntToGray(int value) 
    { 
     var bits = new BitArray(new[] { value }); 
     return BinaryToGray(bits); 
    } 

    public static byte GrayToByte(BitArray gray) 
    { 
     var binary = GrayToBinary(gray); 
     return BinaryToByte(binary); 
    } 

    public static int GrayToInt(BitArray gray) 
    { 
     var binary = GrayToBinary(gray); 
     return BinaryToInt(binary); 
    } 

    /// <summary> 
    ///  Returns the bits as string of '0' and '1' (LSB is right) 
    /// </summary> 
    /// <param name="bits"></param> 
    /// <returns></returns> 
    public static string AsString(this BitArray bits) 
    { 
     var sb = new StringBuilder(bits.Length); 
     for (var i = bits.Length - 1; i >= 0; i--) 
     { 
      sb.Append(bits[i] ? "1" : "0"); 
     } 
     return sb.ToString(); 
    } 

    public static IEnumerable<bool> Bits(this BitArray bits) 
    { 
     return bits.Cast<bool>(); 
    } 

    public static bool[] AsBoolArr(this BitArray bits, int count) 
    { 
     return bits.Bits().Take(count).ToArray(); 
    } 
} 
+0

Ciò è utile ma manca una funzione 'IntToBinary'. E se ogni funzione fosse indipendente da altre funzioni, sarebbe stato grandioso. –

+0

IntToBinary: 'var bits = new BitArray (new [] {value});' – BerndK