SORU
8 NİSAN 2010, PERŞEMBE


'ne s/C En basit şekilde bir bit bayt sırasını tersine çevirmek için?

Bir bayt bit sırasını ters çevirmek için birden fazla yol olsa da, "" uygulamak için bir geliştirici için. en basit ne çok merak ediyorum Ve ters çevirerek yani

1110 -> 0111
0010 -> 0100

Buna benzer, ama this yinelenen PHP bir soru değil.

Buna benzer, ama yinelenen this C Bir soru değil. Bu soru bir geliştirici tarafından uygulanması en kolay yöntem istiyor. "En İyi Algoritma" bellek ve işlemci performansı ile ilgilidir.

CEVAP
8 NİSAN 2010, PERŞEMBE


Bir arama tablosu en basit yöntemlerden biri olmalı bence. Ancak, tam bir arama tablosu gerekmez.

//Index 1==0b0001 => 0b1000
//Index 7==0b0111 => 0b1110
//etc
static unsigned char lookup[16] = {
0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf, };

uint8_t reverse(uint8_t n) {
   // Reverse the top and bottom nibble then swap them.
   return (lookup[n&0b1111] << 4) | lookup[n>>4];
}

// Detailed breakdown of the math
//    lookup reverse of bottom nibble
//  |         grab bottom nibble
//  |       |          move bottom result into top nibble
//  |       |        |       combine the bottom and top results 
//  |       |        |     |   lookup reverse of top nibble
//  |       |        |     | |         grab top nibble
//  V       V        V     V V       V
// (lookup[n&0b1111] << 4) | lookup[n>>4]

Bu oldukça basit ve görsel olarak kodu doğrulamak için.
Sonuçta bu bile tam bir tablo daha hızlı olabilir. Bit arith ucuz ve tablo kolayca önbellek bir çizgi üzerinde uyuyor.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • finalcall07

    finalcall07

    11 NİSAN 2008
  • International Secret Agents Concerts

    Internationa

    2 Aralık 2008
  • spectragirlz16's channel

    spectragirlz

    22 Ocak 2012