-
Bits & Bytes
Hello all ! I was just wondering if anyone migth have any ideas about how i migth fix this problem. The problem is in this testfunction. I am reading in 4 bytes into a unsigned integer, but the data is backwards and i am tring to flip it around to display the correct vaules. After reading in 4 bytes and displaying
the data the output is " 42010000 ". However i was tring to flip the bits so it would be displayed as " 00000142 ". Does anyone have any ideas that migth help me here ???
void Test::testFunction(ifstream &inFile)
{
unsigned int buffer;
inFile.seekg(6240, ios::beg);
inFile.read(reinterpret_cast<char *>(&buffer), 4);
data = (buffer<<8)|buffer; // <--- not working here
}
thanks for any help guys :)
-
I'm not sure this is the simplest way, but it does the job.
Code:
buffer = (buffer&0x000000ff)<<24 | (buffer&0x0000ff00)<<8 | (buffer&0x00ff0000)>>8 | (buffer&0xff000000)>>24;
-
You can also use a union and bit fields.
Code:
unsigned int flip(unsigned int a)
{
union int_byte
{
unsigned int i;
struct
{
int b1: 8;
int b2: 8;
int b3: 8;
int b4: 8;
} byte;
};
int_byte temp, result;
temp.i = a;
result.byte.b1 = temp.byte.b4;
result.byte.b2 = temp.byte.b3;
result.byte.b3 = temp.byte.b2;
result.byte.b4 = temp.byte.b1;
return result.i;
}
And to call function:
data = flip(buffer);
-
That was an excellent explanation, Swoop. Good work!
-
-
I dunno if this is what u r looking for but it works for flippin things around I only breifly read ur post. Here it goes.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int main()
{ char in[51], out[51];
int i=0,x,h=0;
cout<<"Input Some Text No Less Then 50 Chars\n";
cin>>in;
x=strlen(in);
while (h<x)//between here
{ out[i]=in[x];
cout<<out[i];
x--;
i++;
h++;
}//and here
system("PAUSE");
return 0;
}
I never tried to compile it so It may not work. I didn't even go over it really. It s just to give you the idea of what I mean;
-
Here's a flip, using only shifts.
Code:
buffer = buffer<<24 | buffer>>8<<24>>8 | buffer<<8>>24<<8 | buffer>>24;
-
Hey guys ... thanks for the replies :)
Swoopy i tried your first solution, and it worked great for any variable. However one problem, i dont understand what its really doing, hehe. Guess i will have to find some good information on the net about bit shifting (since all books i have only give a brief introdution on that topic). As for the union and bit fields i havent
had a chance to try that one yet. Anyways i just seen your lastest post with fliping using only shifts, that one looks a little more easy to understand compared to the "0X0000ff00" solution.
But, i have a question ... are the 8 and 24 magic numbers used because of a 32bit OS ? will this still work on a 16bit OS ? I see why 8 would be used, just not sure why the value 24 is used here.
Thanks again for the help :)
-
>But, i have a question ... are the 8 and 24 magic numbers used because of a 32bit OS ?
Yes. This code assumes an int is 4 bytes (32 bits). In your case it appears each byte needs to be moved as follows:
Code:
int before flip
----------------
1 2 3 4
----------------
int after flip
----------------
4 3 2 1
----------------
So,
byte 1 -> byte 4
byte 2 -> byte 3
byte 3 -> byte 2
byte 4 -> byte 1
If you notice, to get byte 1 to byte 4, you need to shift three bytes (24 bits). byte 2 only need to be shifted one byte (8 bits). Hence the numbers 24 and 8.
If your int is 16 bits, the only thing possible is to swap each byte:
buffer = (buffer>>8) | (buffer<<8)
As a matter of fact, my computer at work uses a 16 bit int, so I used a long (32 bits) to test the shift-only method.
The idea of the shifting of course is to shift each byte to the new position.
Hope this helps