Bits & Bytes

This is a discussion on Bits & Bytes within the C++ Programming forums, part of the General Programming Boards category; Hello all ! I was just wondering if anyone migth have any ideas about how i migth fix this problem. ...

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    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

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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;

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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);

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    That was an excellent explanation, Swoop. Good work!
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Thanks Sebasti !

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208
    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;
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a flip, using only shifts.
    Code:
       buffer = buffer<<24 | buffer>>8<<24>>8 | buffer<<8>>24<<8 | buffer>>24;

  8. #8
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    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

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. trying to convert system bytes into bits and nibbles.
    By kraze_505 in forum C Programming
    Replies: 11
    Last Post: 01-25-2006, 01:27 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21