Make array of bools out of Ruby integer bits
来源:互联网 发布:ip扫描软件 编辑:程序博客网 时间:2024/05/20 01:35
Okay, so I was wondering if I could create an array of Boolean values from an integer by using its bits. For example, if I wanted to use 5, which is 101 in binary, is there any reasonably easy way that I could get this in something of the form:
[true, false, true]
This would work:
num = 5(0...num.bit_length).map { |i| num[i] == 1 }#=> [true, false, true]
or
(0...num.bit_length).map { |i| num[i] == 1 }.reverse#=> [true, false, true]
depending on the desired bit order.
Try this:
x = 5.to_s(2).split(//).collect do |n| n == "1"endp x
0 0
- Make array of bools out of Ruby integer bits
- How to find number of a specific integer in an array of Fixnums ruby
- Number of Bits in a Decimal Integer
- Reverse bits of an unsigned integer
- Reverse bits of a given 32 bits unsigned integer
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- Number of 1 Bits && Rotate Array && Reverse Bits
- leetcode之Number of 1 Bits,Reverse Bits,Rotate Array
- Ruby converting array of hashes to array of arrays
- integer operation result is out of range
- PostgreSQL integer out of range一例
- PostgreSQL问题解决--- integer out of range
- 数组溢出 Array out of bounds
- Sorting an array of hash in Ruby
- Ruby Group Parts of An Array
- Extracting Data from array of hashes Ruby
- missing ranges of sorted integer array
- Counting-the-number-of-set-bits-in-an-integer
- 设计模式——结构型模式
- 京东商城登录逻辑分析,实现程序登录京东商城
- 关于华硕错误0x0000009c蓝屏
- 介绍MFC框架中涉及到的设计模式(二)
- POJ 1845 Sumdiv(快速幂取模+快速分解因式)
- Make array of bools out of Ruby integer bits
- NY25 A Famous Music Composer
- adaboost学习
- CSS自适应布局(左右固定 中间自适应或者右侧固定 左侧自适应)
- 除法运算的注意点
- HDU 1875(最小生成树)
- __repr__与__str__
- POJ2406 KMP前缀周期
- java并发编程(5)--多个线程共享数据和对象的方式