# Convert a n binary number to /n

**URL:** <https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042>\
**Category:** Ruby\
**Created:** [August 14, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042 "2007-08-14T16:42:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [August 14, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042/1 "2007-08-14T16:42:17Z")

</div>

Hi,

If my number is 1111, I would like to had 0000.  
If my number is 1001, I would like to had 0110.  
If my number is 0011, I would like to had 1100.

Of cours I know we can use XOR operator, but I would like to allow Ruby  
to do it more simply, just by using a function as simple as possible. I  
a sure this kind of function is yet setup in Ruby, but I don’t know what  
is his name.

Thank you for help.

Zang

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [August 14, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042/2 "2007-08-14T16:48:24Z")

</div>

On Aug 14, 2007, at 4:42 PM, Zangief I. wrote:

> is his name.  
> A solution:

irb(main):003:0\> “0011”.tr(“01”, “10”)  
=\> “1100”

– fxn

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [August 15, 2007, 12:41am UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042/3 "2007-08-15T00:41:42Z")

</div>

Xavier N. wrote:

> irb(main):003:0\> “0011”.tr(“01”, “10”)  
> =\> “1100”

Or, if you’re working with integers (as opposed to strings), then you  
can use the bitwise negation operator (~) and avoid the overhead of  
string conversions altogether:

> > 0b1010  
> > =\> 10
> 
> > ~0b1010  
> > =\> -11
> 
> > ~(~0b1010)  
> > =\> 10

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [August 15, 2007, 12:56am UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042/4 "2007-08-15T00:56:00Z")

</div>

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [August 15, 2007, 3:10am UTC](https://www.ruby-forum.com/t/convert-a-n-binary-number-to-n/112042/5 "2007-08-15T03:10:33Z")

</div>

Zangief I. wrote:

> If my number is 1111, I would like to had 0000.  
> If my number is 1001, I would like to had 0110.  
> If my number is 0011, I would like to had 1100.  
> Thanks 🙂

sprintf("%04d", 1111- 1111) -\> 0000  
sprintf("%04d", 1111- 1011) -\> 0100  
sprintf("%04d", 1111- 1001) -\> 0110  
sprintf("%04d", 1111- 0011) -\> 1102 (oops - it treats the second  
value as octal)  
sprintf("%04d", 1111- “0011”.to\_i) -\> 1100

Todd
