Ruby/Language Basics/Bitwise Operations

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:59, 13 сентября 2010

Содержание

bitwise and

1011 & 1010 # bitwise and => 1010



bitwise exclusive or

1011 ^ 1010 # bitwise exclusive or => 1



bitwise not or complement

~1011 # bitwise not or complement => -1012



bitwise or

1011 | 1010 # bitwise or => 1011



do bitwise operations in Ruby.

A bitwise operation operates on each bit, bit for bit, rather than on the numeral as a single unit. 
Bitwise operations are often faster than regular arithmetic operations. 
 
puts ~1011 # bitwise not or complement
puts 1011 | 1010 # bitwise or
puts 1011 & 1010 # bitwise and
puts 1011 ^ 1010 # bitwise exclusive or
puts 1011 << 1 # shift left
puts 1011 >> 1 # shift right