# How to manipulate binary file

**URL:** <https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849>\
**Category:** Ruby\
**Created:** [February 8, 2007, 3:14am UTC](https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849 "2007-02-08T03:14:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minxing\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Minxing\_L](https://www.ruby-forum.com/u/Minxing_L)\
**Post date:** [February 8, 2007, 3:14am UTC](https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849/1 "2007-02-08T03:14:13Z")

</div>

I love Ruby very much and now Ruby is my first language.But now I meet a  
problem on Ruby.I want to do as follows:  
1:Open a file in binary mode  
2:Shift the bit in a algorithm  
3:Save the file

I don’t know how to shift the bit stream in Ruby.Can any one give me an  
advice???

---

<div class="post-metadata">

**Author:** ![Minxing\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Minxing\_L](https://www.ruby-forum.com/u/Minxing_L)\
**Post date:** [February 8, 2007, 6:11am UTC](https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849/2 "2007-02-08T06:11:23Z")

</div>

On Feb 7, 2007, at 9:13 PM, Minxing L. wrote:

> I love Ruby very much and now Ruby is my first language.But now I  
> meet a  
> problem on Ruby.I want to do as follows:  
> 1:Open a file in binary mode  
> 2:Shift the bit in a algorithm  
> 3:Save the file
> 
> I don’t know how to shift the bit stream in Ruby.Can any one give  
> me an  
> advice???

The bit-shift operator is \<\<. Example:

`
n = 1
n = n < 4 (left shift)
n < 2 (right shift)
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Minxing\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Minxing\_L](https://www.ruby-forum.com/u/Minxing_L)\
**Post date:** [February 8, 2007, 6:16am UTC](https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849/3 "2007-02-08T06:16:23Z")

</div>

On Feb 7, 2007, at 9:13 PM, Minxing L. wrote:

> I love Ruby very much and now Ruby is my first language.But now I  
> meet a  
> problem on Ruby.I want to do as follows:  
> 1:Open a file in binary mode  
> 2:Shift the bit in a algorithm  
> 3:Save the file
> 
> I don’t know how to shift the bit stream in Ruby.Can any one give  
> me an  
> advice???

I should also mention that \>\> is a bit-shift operator, too. Works the  
opposite of \<\<.

`
n = 4
n = n >> 2 # => 1
n >> -1 # => 2
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Minxing\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Minxing\_L](https://www.ruby-forum.com/u/Minxing_L)\
**Post date:** [February 9, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/how-to-manipulate-binary-file/84849/4 "2007-02-09T19:24:14Z")

</div>

On 2/7/07, Minxing L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I love Ruby very much and now Ruby is my first language.But now I meet a  
> problem on Ruby.I want to do as follows:  
> 1:Open a file in binary mode  
> 2:Shift the bit in a algorithm  
> 3:Save the file
> 
> I don’t know how to shift the bit stream in Ruby.Can any one give me an  
> advice???

As mentioned before, you can use the \>\> and \<\< operators to perform  
bit shift operations, but these only work with integers. You will need  
to use the pack and unpack methods to convert the strings you read  
from the file into integers.

str = IO.read(‘myfile’)  
ary = str.unpack(‘N\*’)  
ary.map! {|int| int \>\> 2}  
str = ary.pack(‘N\*’)  
File.open(‘anotherfile’,‘w’) {|fd| fd.write str}

This example assumes you have a file full of 32-bit integers in  
big-endian format. It will shift each integer 2 bits to the right.

Blessings,  
TwP
