[ create a new paste ] login | about

Link: http://codepad.org/Kkm8uPyR    [ raw code | output | fork ]

C, pasted on Oct 5:
#include <stdio.h>
#include <assert.h>

static unsigned char
reverse_bits8(unsigned char v)
{
    v = ((v >> 1) & 0x55) | ((v & 0x55) << 1);
    v = ((v >> 2) & 0x33) | ((v & 0x33) << 2);
    v = ( v >> 4 ) | ( v << 4);
    return v;
}
static void
test_reverse_bit8(void)
{
    int i;
    unsigned char x;
    for (i = 0; i <= 0xff; i++) {
        x = (unsigned char)i;
        assert(x == reverse_bits8(reverse_bits8(x)));
    }
}

static void
output_binary_x(unsigned int n, int x)
{
    int i;
    for (i = x; i >= 0; i--)
        putchar("01"[!!((n >> i) & 1)]);
}

static void
output_binary8(unsigned char n)
{
    output_binary_x((unsigned char)n, 7);
}

static void test(void)
{
    test_reverse_bit8();
}


int main(void)
{
    int i, j;
    test();
    for (i = 0; i <= 0xff; i++) {
        unsigned char A;
        A = (unsigned char)i;
        output_binary8(A);
        printf("(%3d):", i);
        for (j = 0; j <= 0xff; j++) {
            unsigned char x;
            x = (unsigned char)j;
            x = x + reverse_bits8(x);
            if (x == A) {
                output_binary8(j);
                putchar(' ');
            }
        }
        printf("\n");
    }
    return 0;
}


Output:
00000000(  0):00000000 
00000001(  1):
00000010(  2):10000001 
00000011(  3):
00000100(  4):
00000101(  5):01000011 11000010 
00000110(  6):
00000111(  7):
00001000(  8):
00001001(  9):
00001010( 10):
00001011( 11):00100111 01100101 10100110 11100100 
00001100( 12):
00001101( 13):
00001110( 14):
00001111( 15):
00010000( 16):
00010001( 17):
00010010( 18):
00010011( 19):
00010100( 20):
00010101( 21):
00010110( 22):
00010111( 23):00011111 00111011 01011101 01111001 10011110 10111010 11011100 11111000 
00011000( 24):00001000 00010000 
00011001( 25):
00011010( 26):10001001 10010001 
00011011( 27):
00011100( 28):
00011101( 29):01001011 01010011 11001010 11010010 
00011110( 30):
00011111( 31):
00100000( 32):
00100001( 33):
00100010( 34):
00100011( 35):00101111 00110111 01101101 01110101 10101110 10110110 11101100 11110100 
00100100( 36):00000100 00100000 
00100101( 37):
00100110( 38):10000101 10100001 
00100111( 39):
00101000( 40):
00101001( 41):01000111 01100011 11000110 11100010 
00101010( 42):
00101011( 43):
00101100( 44):
00101101( 45):
00101110( 46):
00101111( 47):
00110000( 48):00011000 
00110001( 49):
00110010( 50):10011001 
00110011( 51):
00110100( 52):
00110101( 53):01011011 11011010 
00110110( 54):
00110111( 55):
00111000( 56):
00111001( 57):
00111010( 58):
00111011( 59):00111111 01111101 10111110 11111100 
00111100( 60):00001100 00010100 00101000 00110000 
00111101( 61):
00111110( 62):10001101 10010101 10101001 10110001 
00111111( 63):
01000000( 64):
01000001( 65):01001111 01010111 01101011 01110011 11001110 11010110 11101010 11110010 
01000010( 66):00000010 01000000 
01000011( 67):
01000100( 68):10000011 11000001 
01000101( 69):
01000110( 70):
01000111( 71):
01001000( 72):00100100 
01001001( 73):
01001010( 74):10100101 
01001011( 75):
01001100( 76):
01001101( 77):01100111 11100110 
01001110( 78):
01001111( 79):
01010000( 80):
01010001( 81):
01010010( 82):
01010011( 83):
01010100( 84):00011100 00111000 
01010101( 85):
01010110( 86):10011101 10111001 
01010111( 87):
01011000( 88):
01011001( 89):01011111 01111011 11011110 11111010 
01011010( 90):00001010 00010010 01001000 01010000 
01011011( 91):
01011100( 92):10001011 10010011 11001001 11010001 
01011101( 93):
01011110( 94):
01011111( 95):
01100000( 96):00101100 00110100 
01100001( 97):
01100010( 98):10101101 10110101 
01100011( 99):
01100100(100):
01100101(101):01101111 01110111 11101110 11110110 
01100110(102):00000110 00100010 01000100 01100000 
01100111(103):
01101000(104):10000111 10100011 11000101 11100001 
01101001(105):
01101010(106):
01101011(107):
01101100(108):
01101101(109):
01101110(110):
01101111(111):
01110000(112):
01110001(113):
01110010(114):00011010 01011000 
01110011(115):
01110100(116):10011011 11011001 
01110101(117):
01110110(118):
01110111(119):
01111000(120):00111100 
01111001(121):
01111010(122):10111101 
01111011(123):
01111100(124):
01111101(125):01111111 11111110 
01111110(126):00001110 00010110 00101010 00110010 01001100 01010100 01101000 01110000 
01111111(127):
10000000(128):10001111 10010111 10101011 10110011 11001101 11010101 11101001 11110001 
10000001(129):00000001 10000000 
10000010(130):
10000011(131):
10000100(132):01000010 
10000101(133):
10000110(134):11000011 
10000111(135):
10001000(136):
10001001(137):
10001010(138):00100110 01100100 
10001011(139):
10001100(140):10100111 11100101 
10001101(141):
10001110(142):
10001111(143):
10010000(144):
10010001(145):
10010010(146):
10010011(147):
10010100(148):
10010101(149):
10010110(150):00011110 00111010 01011100 01111000 
10010111(151):
10011000(152):10011111 10111011 11011101 11111001 
10011001(153):00001001 00010001 10001000 10010000 
10011010(154):
10011011(155):
10011100(156):01001010 01010010 
10011101(157):
10011110(158):11001011 11010011 
10011111(159):
10100000(160):
10100001(161):
10100010(162):00101110 00110110 01101100 01110100 
10100011(163):
10100100(164):10101111 10110111 11101101 11110101 
10100101(165):00000101 00100001 10000100 10100000 
10100110(166):
10100111(167):
10101000(168):01000110 01100010 
10101001(169):
10101010(170):11000111 11100011 
10101011(171):
10101100(172):
10101101(173):
10101110(174):
10101111(175):
10110000(176):
10110001(177):00011001 10011000 
10110010(178):
10110011(179):
10110100(180):01011010 
10110101(181):
10110110(182):11011011 
10110111(183):
10111000(184):
10111001(185):
10111010(186):00111110 01111100 
10111011(187):
10111100(188):10111111 11111101 
10111101(189):00001101 00010101 00101001 00110001 10001100 10010100 10101000 10110000 
10111110(190):
10111111(191):
11000000(192):01001110 01010110 01101010 01110010 
11000001(193):
11000010(194):11001111 11010111 11101011 11110011 
11000011(195):00000011 01000001 10000010 11000000 
11000100(196):
11000101(197):
11000110(198):
11000111(199):
11001000(200):
11001001(201):00100101 10100100 
11001010(202):
11001011(203):
11001100(204):01100110 
11001101(205):
11001110(206):11100111 
11001111(207):
11010000(208):
11010001(209):
11010010(210):
11010011(211):
11010100(212):
11010101(213):00011101 00111001 10011100 10111000 
11010110(214):
11010111(215):
11011000(216):01011110 01111010 
11011001(217):
11011010(218):11011111 11111011 
11011011(219):00001011 00010011 01001001 01010001 10001010 10010010 11001000 11010000 
11011100(220):
11011101(221):
11011110(222):
11011111(223):
11100000(224):
11100001(225):00101101 00110101 10101100 10110100 
11100010(226):
11100011(227):
11100100(228):01101110 01110110 
11100101(229):
11100110(230):11101111 11110111 
11100111(231):00000111 00100011 01000101 01100001 10000110 10100010 11000100 11100000 
11101000(232):
11101001(233):
11101010(234):
11101011(235):
11101100(236):
11101101(237):
11101110(238):
11101111(239):
11110000(240):
11110001(241):
11110010(242):
11110011(243):00011011 01011001 10011010 11011000 
11110100(244):
11110101(245):
11110110(246):
11110111(247):
11111000(248):
11111001(249):00111101 10111100 
11111010(250):
11111011(251):
11111100(252):01111110 
11111101(253):
11111110(254):11111111 
11111111(255):00001111 00010111 00101011 00110011 01001101 01010101 01101001 01110001 10001110 10010110 10101010 10110010 11001100 11010100 11101000 11110000 


Create a new paste based on this one


Comments: