# Thread: Working with Bit Operators to Reverse an Octal Dump

1. ## 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. Boy would I love to get my hands on the teacher who's giving out these screwball assignments...

Homework Policy

3. > 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)

4. Originally Posted by PincPleasure
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.
Originally Posted by PincPleasure
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.
Originally Posted by PincPleasure
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.
Originally Posted by PincPleasure
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.

5. 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. Originally Posted by PincPleasure
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. Originally Posted by anduril462
• An octal digit has exactly 3 bits of information
• Two bytes is 16 bits.
Here is another one for you: 16 / 3 = ? r ?

Quzah.

8. Originally Posted by quzah
Here is another one for you: 16 / 3 = ? r ?

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