Skip to content

Commit e5da7d3

Browse files
authoredJan 21, 2018
Add Int#bits_set? method (#5619)
1 parent e83e894 commit e5da7d3

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed
 

Diff for: ‎src/colorize.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ struct Colorize::Object(T)
318318
unless mode_is_default
319319
# Can't reuse MODES constant because it has bold/bright duplicated
320320
{% for name in %w(bold dim underline blink reverse hidden) %}
321-
if (@mode & MODE_{{name.upcase.id}}_FLAG) != 0
321+
if @mode.bits_set? MODE_{{name.upcase.id}}_FLAG
322322
io << ";" if printed
323323
io << MODE_{{name.upcase.id}}
324324
printed = true

Diff for: ‎src/crystal/system/unix/file_descriptor.cr

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module Crystal::System::FileDescriptor
2727
end
2828

2929
private def system_blocking?
30-
fcntl(LibC::F_GETFL) & LibC::O_NONBLOCK == 0
30+
flags = fcntl(LibC::F_GETFL)
31+
!flags.bits_set? LibC::O_NONBLOCK
3132
end
3233

3334
private def system_blocking=(value)
@@ -43,7 +44,7 @@ module Crystal::System::FileDescriptor
4344

4445
private def system_close_on_exec?
4546
flags = fcntl(LibC::F_GETFD)
46-
(flags & LibC::FD_CLOEXEC) == LibC::FD_CLOEXEC
47+
flags.bits_set? LibC::FD_CLOEXEC
4748
end
4849

4950
private def system_close_on_exec=(arg : Bool)

Diff for: ‎src/debug/mach_o.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ module Debug
117117
end
118118

119119
def abi64?
120-
cputype.value & ABI64 == ABI64
120+
cputype.value.bits_set? ABI64
121121
end
122122

123123
def endianness
@@ -292,11 +292,11 @@ module Debug
292292
end
293293

294294
def pext?
295-
(value & PEXT) == PEXT
295+
value.bits_set? PEXT
296296
end
297297

298298
def ext?
299-
(value & EXT) == EXT
299+
value.bits_set? EXT
300300
end
301301

302302
{% for flag in %w(UNDF ABS SECT PBUD INDR) %}

Diff for: ‎src/enum.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct Enum
100100
found = false
101101
{% for member in @type.constants %}
102102
{% if member.stringify != "All" %}
103-
if {{@type}}::{{member}}.value != 0 && (value & {{@type}}::{{member}}.value) == {{@type}}::{{member}}.value
103+
if {{@type}}::{{member}}.value != 0 && value.bits_set? {{@type}}::{{member}}.value
104104
io << " | " if found
105105
io << {{member.stringify}}
106106
found = true

Diff for: ‎src/int.cr

+11
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ struct Int
296296
self >> bit & 1
297297
end
298298

299+
# Returns `true` if all bits in *mask* are set on `self`.
300+
#
301+
# ```
302+
# 0b0110.bits_set?(0b0110) # => true
303+
# 0b1101.bits_set?(0b0111) # => false
304+
# 0b1101.bits_set?(0b1100) # => true
305+
# ```
306+
def bits_set?(mask)
307+
(self & mask) == mask
308+
end
309+
299310
def gcd(other : Int)
300311
self == 0 ? other.abs : (other % self).gcd(self)
301312
end

0 commit comments

Comments
 (0)
Please sign in to comment.