# Re: Bitwise operations

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

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 19, 2006, 1:43am UTC](https://www.ruby-forum.com/t/re-bitwise-operations/72804/1 "2006-10-19T01:43:54Z")

</div>

From: Rob L.

> 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 ?

Strings in Ruby are, as you say, series of 8-bit bytes. The String#[]  
operator when called with a single integer index returns the numeric  
value of the requested byte:

irb(main):001:0\> s = “\xf7\x23”  
=\> “\367#”  
irb(main):002:0\> s[0]  
=\> 247  
irb(main):003:0\> s[0].to\_s(16)  
=\> “f7”

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 19, 2006, 1:47am UTC](https://www.ruby-forum.com/t/re-bitwise-operations/72804/2 "2006-10-19T01:47:54Z")

</div>

Gavin K. wrote:

> Strings in Ruby are, as you say, series of 8-bit bytes. The String#[]  
> operator when called with a single integer index returns the numeric  
> value of the requested byte:

Reputedly about to change in Ruby 2, indexing a String will return a  
single-character string. I think there’ll be some more clean separation  
between methods operating on a String as a (possibly Unicode) text  
string or as binary data. I’m still personally not quite happy with the  
semantics of Strings being overloaded like this, but it’s probably the  
most compatible way.

David V.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 19, 2006, 2:59am UTC](https://www.ruby-forum.com/t/re-bitwise-operations/72804/3 "2006-10-19T02:59:59Z")

</div>

On 10/18/06, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gavin K. wrote:
> 
> > Strings in Ruby are, as you say, series of 8-bit bytes. The String#[]  
> > operator when called with a single integer index returns the numeric  
> > value of the requested byte:  
> > Reputedly about to change in Ruby 2, indexing a String will return a  
> > single-character string. I think there’ll be some more clean separation  
> > between methods operating on a String as a (possibly Unicode) text  
> > string or as binary data. I’m still personally not quite happy with the  
> > semantics of Strings being overloaded like this, but it’s probably the  
> > most compatible way.

Fortunately, it will be controlled where the encoding of the binary  
blob that is the string is treated as a view on the data. Thus, when  
the encoding is UTF-8, Ruby will try to interpret the characters  
according to UTF-8 rules. When the encoding is none (or raw, or  
something), Ruby won’t try to interpret the characters at all.

-austin
