Gray Code in Binär Code umwandeln

Gast #1424235
Lesenswert?

Hallo,

würde gerne einen 4 Bit Gray Code in einen Binär Code umwandeln. Ich 
habe den Code über eine Gray-Code Scheibe in meinen Controler eingelesen 
und in einer 8 Bit Char Variable abgespeichert. Die oberen 4 Bit habe 
ich ausmaskiert und würde nun gerne die 4 Bit Gray über einen 
Algorythmus in 4 Bit binär umwandeln. Kann mir vielleicht jamand helfen?
Vielen Dank im Voraus.

Gruß, DAni
Gast #1424269
Lesenswert?

Hallo,

ich würde die umwandlung gerne mit einem allgorythmus machen. Ich könnte 
den binärwert auch über einen swich case zuweisen das will ich aber 
nicht.

das habe ich gefunden: Binärcode X1 --> Graycode: X4 = ((X1*2) XOR X1) / 
2

ich muss das wahrscheinlich nach x1 auflösen. hab aber hier auch schon 
probleme.

oder gibts vielleicht auch was einfacheres?
Gast #1424746
Lesenswert?

Hi,
Here the a 32 Bit and a 4 Bit variant. The 4 Bit variant needs only one 
CPU cycle more than a table based approach.

Jochen

static uint32_t
GrayDecodeU32(uint32_t to_decode) {
        uint32_t result = to_decode ^ (to_decode >> 16);
        result ^= result >> 8;
        result ^= result >> 4;
        result ^= result >> 2;
        result ^= result >> 1;
        return result;
}

uint8_t
GrayDecodeU4(uint8_t t) {
         t = t ^ (t >> 2);
         return t ^ (t >> 1);
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren