Class: Fixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/bitwise.rb

Instance Method Summary collapse

Instance Method Details

#bit?(bit) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bitwise.rb', line 20

def bit?(bit)
	if bit == 7
		value = 0 if self & 128 == 0
		value = 1 if self & 128 != 0
	elsif bit == 6
		value =  0 if self & 64 == 0
		value =  1 if self & 64 != 0
	elsif bit == 5
		value =  0 if self & 32 == 0
		value =  1 if self & 32 != 0
	elsif bit == 4
		value = 0 if self & 16 == 0
		value = 1 if self & 16 != 0
	elsif bit == 3
		value =  0 if self & 8 == 0
		value =  1 if self & 8 != 0
	elsif bit == 2
		value = 0 if self & 4 == 0
		value =  1 if self & 4 != 0
	elsif bit == 1
		value =  0 if self & 2 == 0
		value =  1 if self & 2 != 0
	elsif bit == 0
		value =  0 if self & 1 == 0
		value = 1 if self & 1 != 0
	end

	return value
end

#clear_bit(bit) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bitwise.rb', line 63

def clear_bit(bit)
	self & case bit
		when 7 then 127
		when 6 then 191
		when 5 then 223
		when 4 then 239
		when 3 then 247
		when 2 then 251
		when 1 then 253
		when 0 then 254
	end
end

#set_bit(bit) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bitwise.rb', line 50

def set_bit(bit)
	self | case bit
		when 7 then 128
		when 6 then 64
		when 5 then 32
		when 4 then 16
		when 3 then 8
		when 2 then 4
		when 1 then 2
		when 0 then 1
	end
end