Click here to Skip to main content
15,881,744 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
hi all,

I was asked by a question in a interview, how to reverse a int number byte by byte

for example:

I am having a integer value

int a=0x12 34 56 78;

i want to reverse it by

b=0x87 65 43 21;


can anyone plz give a logic for this



Thanks
shan
Posted
Updated 9-Jul-20 17:23pm
v2
Comments
phil.o 4-Mar-14 3:20am    
Not clear: do you want to reverse it bit by bit or byte by byte? Because that is not the same at all.
shan bala 4-Mar-14 3:31am    
Hi phil,

sorry,

bit by bit

and also byte by byte

int a=0x12 34 56 78

o/p
b=0x 78 56 34 12
OriginalGriff 4-Mar-14 3:43am    
Please note: that isn't a bit-by bit swap: it's a nibble-by-nibble swap.
The bit-by-bit swap of 8 in 1, 7 is E, 6 is 6, 5 is A, and so forth.
Vedat Ozan Oner 4-Mar-14 17:02pm    
this question really took attention :)

There are a couple of ways to do this: but the simplest is probably to use pointers:
C++
#define byte unsigned char
int main()
    {
    int a = 0x12345678;
    int b;
    byte* pa = (byte*)&a;
    byte* pb = (byte*)&b;
    *(pb + 0) = *(pa + 3);
    *(pb + 1) = *(pa + 2);
    *(pb + 2) = *(pa + 1);
    *(pb + 3) = *(pa + 0);
    b = ((b >> 4) & 0x0F0F0F0F) | ((b & 0x0F0F0F0F) << 4); //ADDED This for nibble swapping
    printf( "%x:%x\n",a, b  );
    }


[edit]:doh: Didn't notice the nibble swap as well![/edit]
 
Share this answer
 
v2
Comments
shan bala 4-Mar-14 3:57am    
hi,

Thanks, its working, can you explain the logic
OriginalGriff 4-Mar-14 4:25am    
You are kidding, right?
You don't understand what that is doing?
It's about the simplest way to do it, there is no complexity there at all...
Maciej Los 4-Mar-14 17:54pm    
+5!
C
int rev_nibbles(int x)
{
  int y = 0;
  while (x)
  {
    y <<= 4;
    y |= x & 0xF;
    x >>= 4;
  }
  return y;
}
 
Share this answer
 
Comments
Maciej Los 4-Mar-14 17:54pm    
+5!
CPallini 5-Mar-14 3:24am    
Thank you.
in addition to OriginalGriff, bitwise reverse is here:

C#
#include <stdio.h>

int main() {
 int i;

 // set size of it as bit count
 int sizeint=sizeof(int)*8;
 // given number
 unsigned int given=~2;
 // reversed value
 unsigned int reversed=0;
 // bit test
 unsigned int test=1;

 // for each bit
 for(i=0; i<sizeint; i++) {
   // test if that bit set
   if(given&test)
     reversed|=1;
   // next bit
   test<<=1;
   // bit shift to prepare next
   if(test)
    reversed<<=1;
 }
 printf("given=%x, reversed=%x\n", given, reversed);

 return 0;
}
 
Share this answer
 
C
#include <stdio.h>

struct Nibble
{
  unsigned int u0 : 4;
  unsigned int u1 : 4;
  unsigned int u2 : 4;
  unsigned int u3 : 4;
  unsigned int u4 : 4;
  unsigned int u5 : 4;
  unsigned int u6 : 4;
  unsigned int u7 : 4;
};

union IntNibble
{
  int i;
  struct Nibble n;
};

int main()
{
  union IntNibble x,y;

  x.i = 0x12345678;

  y.n.u0 = x.n.u7;
  y.n.u1 = x.n.u6;
  y.n.u2 = x.n.u5;
  y.n.u3 = x.n.u4;
  y.n.u4 = x.n.u3;
  y.n.u5 = x.n.u2;
  y.n.u6 = x.n.u1;
  y.n.u7 = x.n.u0;

  printf("%X\n", y.i);

  return 0;
}
 
Share this answer
 
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
  int x = 0x12345678;
  int y;
  int i;
  char a[9];
  sprintf(a, "%08X", x);
  for (i=0; i<4; ++i)
  {
    char b = a[7-i];
    a[7-i] = a[i];
    a[i] = b;
  }
  y = strtoul(a,NULL,16);
  printf("%X\n", y);
  return 0;
}
 
Share this answer
 
C++
static unsigned long nibble_pivot(unsigned long value)
{
    // word swap (pivot)
    value = (value << 16) | (value >> 16);
    // byte swap
    value = ( (value & 0xFF00FF00UL) >> 8) | ( (value & 0x00FF00FFUL) << 8);
    // nibble swap
    value = ( (value & 0xF0F0F0F0UL) >> 4) | ( (value & 0x00F0F0F0FUL) << 4);
    return value;
}

int main(int argc, char* argv[])
{
    fprintf(stdout, "%08lX -> %08lX\n", 0x12345678UL, nibble_pivot(0x12345678UL));
    getchar();
    return 0;
}
 
Share this answer
 
v2
Gratuitous use of inline assembly ...

C++
static unsigned long nibble_pivot(unsigned long value)
{
    __asm
    {
        mov eax, value;
        rol eax, 16;
        mov edx, eax;
        and eax, 0xFF00FFUL;
        and edx, 0xFF00FF00UL;
        shr edx, 8;
        shl eax, 8;
        or eax, edx;
        mov edx, eax;
        and eax, 0xF0F0F0FUL;
        and edx, 0xF0F0F0F0UL;
        shr edx, 4;
        shl eax, 4;
        or eax, edx;
        mov value, eax
    }
    return value;
}
 
Share this answer
 
Void reverse()
{
unsigned int a=0x12345678;
a=((a&0x0000000F)<<28)| //(0x80 00 00 00) |
((a&0x000000F0)<<20)| //(0x07 00 00 00) |
((a&0x00000F00)<<12)| // (0x00 60 00 00) |
((a&0x0000F000)<<4)| // (0x00 05 00 00) |
((a&0x000F0000)>>4)| // (0x00 00 40 00) |
((a&0x00F00000)>>12)| //(0x00 00 03 00) |
((a&0x0F000000)>>20)| // (0x00 00 00 20) |
((a&0xF0000000)>>28); //(0x00 00 00 01)
printf("%x\n",a);
}
Output:87654321
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900