Like Tree1Likes
  • 1 Post By quzah

Working with Bit Operators to Reverse an Octal Dump

This is a discussion on Working with Bit Operators to Reverse an Octal Dump within the C Programming forums, part of the General Programming Boards category; Alrighty then... I am relatively new to C so forgive any incorrect terminology or whatnot. My question has to do ...

  1. #1
    Registered User PincPleasure's Avatar
    Join Date
    Sep 2011
    Posts
    2

    Working with Bit Operators to Reverse an Octal Dump

    Alrighty then...

    I am relatively new to C so forgive any incorrect terminology or whatnot.

    My question has to do with a homework assignment I have been given. I am not asking anyone to do my assignment or anything of the sort I am just stuck on a part of it Id like advice on.

    Basically the assignment is to take in an octal dump created by the od command and convert it back to text. Catch is we are not allowed to use arrays (ie strings) at all. We are supposed to work with bit operators.

    We have to take a number such as 060523 and convert each digit back into octal form (except first 0, which is basically ignored in this entire process) like this 0110000101010011 and then convert each 8 digits (meaning binary number) back into its ASCII form.

    Ive discovered that if you separate each digit of 060523 so you are only looking at 1 digit at a time, you can retrieve the octal version of it by shifting the bits 5 places to the right and then 5 places to the left.

    My question is how to combine those together without using a string? For the 060523 example, I would have:
    00000 110
    00000 000
    00000 101
    00000 010
    00000 011
    (I put spaces to make it easier to understand)

    but I need it to look like:
    0110000101010011

    Im assuming I need to somehow be able to manipulate the last three bits and then someone concatenate it together with the other 3 bit sets.

    How on earth could this be done with bit operators? If I was able to use arrays I could easily concatenate them together but Im at a total loss on how to do it otherwise.

    I REALLY appreciate any help!!!

    ~B.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Boy would I love to get my hands on the teacher who's giving out these screwball assignments...

    Homework Policy

    We'll help you with your code... but we are not here to do your math assignments for you.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > like this 0110000101010011 and then convert each 8 digits (meaning binary number) back into its ASCII form.
    But each byte is 8 bits, so what you should be looking to do is
    - convert the octal number to a 16-bit int
    - extract 2 bytes, and print each as a character.

    0110000101010011
    Which are bytes 0x61 and 0x53 respectively (aS)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by PincPleasure View Post
    We have to take a number such as 060523 and convert each digit back into octal form (except first 0, which is basically ignored in this entire process) like this 0110000101010011 and then convert each 8 digits (meaning binary number) back into its ASCII form.
    Perhaps understanding why the leading zero is ignored is the first step to solving this riddle.
    Quote Originally Posted by PincPleasure View Post
    Ive discovered that if you separate each digit of 060523 so you are only looking at 1 digit at a time, you can retrieve the octal version of it by shifting the bits 5 places to the right and then 5 places to the left.
    If you realize that each octal digit is made up of 3 bits, it'd be easier to visualize this conversion.
    Quote Originally Posted by PincPleasure View Post
    My question is how to combine those together without using a string? For the 060523 example, I would have:
    00000 110
    00000 000
    00000 101
    00000 010
    00000 011
    (I put spaces to make it easier to understand)

    but I need it to look like:
    0110000101010011
    Hint: Bitwise AND'ing the octal number with 0xff might be a start in the right direction.
    Quote Originally Posted by PincPleasure View Post
    Im assuming I need to somehow be able to manipulate the last three bits and then someone concatenate it together with the other 3 bit sets.

    How on earth could this be done with bit operators? If I was able to use arrays I could easily concatenate them together but Im at a total loss on how to do it otherwise.

    I REALLY appreciate any help!!!

    ~B.
    No need for catenation or arrays, just print each character as it is AND'd with 0xff first; then left shifted by 8 and again AND'd with 0xff. Lather, rinse, repeat for each set of octal digits.
    Last edited by itCbitC; 09-12-2011 at 02:31 PM.

  5. #5
    Registered User PincPleasure's Avatar
    Join Date
    Sep 2011
    Posts
    2
    itCbitC,

    Thanks for your recommendations. After reading your suggestions I was able to figure out how to do what I needed to with combining the octal numbers together. I ended up creating 5 temporary values and shifting each of the three bits that made up the octal number to the correct place and was able to OR them all together =)

    As far as why the leading zero is ignored I have no clue. We never have to convert a number that doesnt start with a 0 and the only examples we were given show the 0 being pretty much ignored. I WISH they would explain these things better. We haven't even gone over bit operators at all yet either and yet they are required for this >.< It didnt matter though, the ORing I did resulted in the zero being up front like its supposed to.

    Again thanks for your wonderful suggestions!!





    Salem,

    Thank you! After I managed to get the desired 16 bits of information together I was able to separate each byte correctly. Haven't gotten them printing yet but I have faith I can figure that out. Your suggestion really helped =)

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Quote Originally Posted by PincPleasure View Post
    As far as why the leading zero is ignored I have no clue. We never have to convert a number that doesnt start with a 0 and the only examples we were given show the 0 being pretty much ignored. I WISH they would explain these things better.
    Maybe they want you to figure this out on your own. Your assumption that the left-most digit is always a zero is wrong, which doesn't help. That leftmost digit could be a 1, if your file contains non-ASCII data. Try dumping a non-text file, like an executable:
    Code:
    $ od -N64 a.out
    0000000 042577 043114 000401 000001 000000 000000 000000 000000
    0000020 000002 000003 000001 000000 101340 004004 000064 000000
    0000040 003504 000000 000000 000000 000064 000040 000010 000050
    0000060 000036 000033 000006 000000 000064 000000 100064 004004

    I'll give you some other hints:
    • An octal digit has exactly 3 bits of information
    • The od utility, by default, dumps your files in 2-byte chunks
    • Two bytes is 16 bits.
    • Nobody said whether the 2-byte chunks od produces are big- or little-endian

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    • An octal digit has exactly 3 bits of information
    • Two bytes is 16 bits.
    Here is another one for you: 16 / 3 = ? r ?


    Quzah.
    itCbitC likes this.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by quzah View Post
    Here is another one for you: 16 / 3 = ? r ?


    Quzah.
    This is brilliant and that recursion was a stroke of genius!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read in octal
    By laxkrzy in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 10:34 PM
  2. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  3. Not interpeting as octal
    By Chewie8 in forum C Programming
    Replies: 3
    Last Post: 02-15-2008, 08:29 AM
  4. octal numbers
    By firefly in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2005, 06:24 AM
  5. Replies: 11
    Last Post: 04-18-2002, 08:56 PM

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