项目作者: almishkawi

项目描述 :
Ruby gem to convert numbers into binaries and binaries into numbers
高级语言: Ruby
项目地址: git://github.com/almishkawi/binary.git
创建时间: 2018-03-06T18:18:04Z
项目社区:https://github.com/almishkawi/binary

开源协议:MIT License

下载


Binary

Simple Ruby gem to convert numbers into binaries & binaries into numbers

Installation

Add this line to your application’s Gemfile:

  1. gem 'binary'

Then run:

  1. $ bundle

Or from the terminal:

  1. $ gem install binary

Usage

  1. require 'binary'

Number to binary:

  1. Binary.binary 2018
  2. Or
  3. 2018.to_b

Output: "11111100010".

Array of numbers to binaries.

  1. Binary.binary([[7,9,11])
  2. Or
  3. [7,9,11].to_b

Output: ["111", "1001", "1011"].

Binary to number:

  1. Binary.number "11111100010"
  2. Or
  3. "11111100010".to_num

Output: 2018.

Array of binaries to numbers:

  1. Binary.number(["111", "1001", "1011"])
  2. Or
  3. ["111", "1001", "1011"].to_num

Output: [7,9,11].

Other methods available:

  1. # count the number of bits in a number's binary
  2. Binary.bits_count 1000
  3. # number of bits in 1000.to_b ("1111101000")

Output: 10.

  1. # count the "1"s in a number's binary
  2. Binary.ones_count 1000
  3. # number of ones in 1000.to_b ("1111101000")

Output: 6.

  1. # count the "0"s in a number's binary
  2. Binary.zeros_count 1000
  3. # number of zeros in 1000.to_b ("1111101000")

Output: 4.

  1. # list of binaries of prime numbers from 2 to 25
  2. Binary.prime 25

Output: ["10", "11", "101", "111", "1011", "1101", "10001", "10011", "10111"].

  1. # Binary representation of a random number between 1 and 100
  2. Binary.random 100

Output: "111".

License

The gem is available as open source under the terms of the MIT License.