Bit fields

This is a discussion on Bit fields within the C Programming forums, part of the General Programming Boards category; Hi! Here's my problem. I want to read a file and then I want to rotate the file's bits. I ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Bit fields

    Hi!

    Here's my problem. I want to read a file and then I want to rotate the file's bits. I know how to rotate bits, but I do not know how to do this with files. Any ideas?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Code:
    FILE *fp = fopen( "file","rb");
    int ch;
    while ( (ch=fgetc(fp)) != EOF ) {
      // rotate ch
    }

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ok, it is working. But I'm trying to find out how to get back the original file. I rotated the file in left for 1 bit, and now if I want to get it back i rotated the file in right for 1 bit - it is not working. Why not?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    OK, I found the problem. The problem was that I used signed chars.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Because when you rotate say

    xyyyyyyy

    left one bit, what you write out the the file is

    yyyyyyy0

    You lost the 'x' bit

    What you need to do is actually write out

    yyyyyyyx

    which you get from say

    ch = (ch << 1) | ( (ch >> 7) & 0x01 )

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy

    Now I found out that if I rotate the file more than 1 time then it is all mixed up. I'll try your code Salem.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Salem

    Please explain what your code means.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Ok, you start with a byte which looks like this

    xyyyyyyy

    To hide it's true value when you write it out to another file, you want it to look like this
    yyyyyyyx

    Ie, everything rotated one bit to the left (and the most significant bit wrapped around to the least significant bit)

    To do this in C, we do this
    ch = ( (ch << 1) & 0xFE ) | ( (ch >> 7) & 0x01 )

    ( (ch << 1) & 0xFE ) repositions all the y bits
    ( (ch >> 7) & 0x01 ) repositions the x bit

    To get back to where we started, we have

    yyyyyyyx

    And we want to get to
    xyyyyyyy
    ch = ( (ch << 7) & 0x80 ) | ( (ch >> 1) & 0x7F )

    ( (ch << 7) & 0x80 ) repositions the x bit
    ( (ch >> 1) & 0x7F ) repositions all the y bits

  9. #9
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    THANK YOU Salem!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  2. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 07:20 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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