Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 54 / 68

Bitwise Operators (AND, OR, XOR, shifts)

In this lesson you will learn the basics of bitwise operators in Ruby, and understand this lower-level way of calculating with numbers as binary. This is for anyone searching for "Ruby bitwise AND OR".

Bitwise operations treat a number as binary and calculate bit by bit. There is & (AND), | (OR), ^ (XOR), and << (left shift), and they show up in permission flags and low-level work.

The sample code applies AND, OR, XOR, and a left shift to the numbers 12 and 10. Work through them one at a time to see how the per-digit results stack up into the final number.

A common stumbling block for beginners is that these operators work on a completely different principle from ordinary arithmetic. They do not come up much in everyday application work, but they are valuable knowledge for performance-critical code.

In memory-constrained environments, managing flags as bits is still genuinely useful. It comes into its own wherever you want several on/off states packed into a single number, such as a permissions system.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)