bits spiegeln

Wie vfl_freak bereits sagte, mußt du einfach alle Bits der Variablen spiegeln.

Die Größe in Bytes der Variablen kannst du mit dem sizeof Operator ermitteln, die Anzahl der Bits die ein Byte besitzt ist in der Konstante CHAR_BIT (#include <limits.h>) enthalten (ein char ist immer ein Byte groß).

Gruß
 
Hallo,

schau mal hier:
C++:
// ReverseBits.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//

#include "stdafx.h"
#include <iostream>

int reverseBits(int input) {

        int output = 0;
        do{
            output <<= 1;
            if(input % 2 != 0){
                output |= 1;
            }
        }while((input = input / 2) > 0);
        
        return output;
    }

int _tmain(int argc, _TCHAR* argv[])
{
    int input = 230; // 11100110
    int output = reverseBits(input);
    std::cout << output << std::endl; // 103 -> 01100111

    return 0;
}
Kann man natürlich auch viel effizienter (viel komplizierter ausdrücken) ... siehe Bit Manipulation in the Art of cumpoter programming...

Gruß Tom
 
Zurück