# Bitwise operations

**URL:** <https://www.ruby-forum.com/t/bitwise-operations/72803>\
**Category:** Ruby\
**Created:** [October 19, 2006, 1:29am UTC](https://www.ruby-forum.com/t/bitwise-operations/72803 "2006-10-19T01:29:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [October 19, 2006, 1:29am UTC](https://www.ruby-forum.com/t/bitwise-operations/72803/1 "2006-10-19T01:29:18Z")

</div>

Hi,

I have a binary packet stored in a string. I also have a packet  
specification and I’d like to print the first byte as a hex value to  
check it corresponds to the expected value from the specification  
(‘7F’). I can’t understand how to access the first byte from a string  
(a series of 8 bit bytes I believe). There don’t seem to be any  
bit-shift operators in Ruby for a string - could anybody advise how I go  
about this ?

Thanks

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [October 19, 2006, 4:55am UTC](https://www.ruby-forum.com/t/bitwise-operations/72803/2 "2006-10-19T04:55:34Z")

</div>

Rob L. wrote:

> Hi,
> 
> I have a binary packet stored in a string. I also have a packet  
> specification and I’d like to print the first byte as a hex value to  
> check it corresponds to the expected value from the specification  
> (‘7F’). I can’t understand how to access the first byte from a string  
> (a series of 8 bit bytes I believe). There don’t seem to be any  
> bit-shift operators in Ruby for a string - could anybody advise how I go  
> about this ?

It would be overkill for just getting the first byte, but you may find  
bit-struct helpful in defining and parsing binary strings.

[http://raa.ruby-lang.org/project/bit-struct/](http://raa.ruby-lang.org/project/bit-struct/)
