Skip to content

YesDrX/bitarray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nimble MIT

bitarray

A simple bitarray library for nim.

Installation

git clone https://github.com/YesDrX/bitarray.git
cd bitarray
nimble install

or

nimble install nimbitarray

Example

import bitarray
when isMainModule:
  var
    a = newBitsArray(70)
    b = newBitsArray(70)
  
  echo "    a = ",a
  echo "    b = ",b
  echo "set bits ..."
  a.setBits(0,1,2,3,4,69)
  b.setBits(6,7,8,9,65)
  echo "    a = ",a
  echo "    b = ",b
  echo "a & b = ", a & b
  echo "a | b = ", a | b
  echo "a ^ b = ", a ^ b
  echo "   ~a = ", ~a
  echo "a.shl(1) =",a.shl(1)
  echo "a.shl(2) =",a.shl(2)
  echo "a.shl(3) =",a.shl(3)
  echo "a.shl(69)=",a.shl(69)
  echo "a.shr(1) =",a.shr(1)
  echo "a.shr(2) =",a.shr(2)
  echo "a.shr(3) =",a.shr(3)
  echo "a.shr(69)=",a.shr(69)

  echo "basic types"
  echo (-7).toBitsArray
  echo (7).toBitsArray
  echo (-1.555).toBitsArray
  echo (1.555).toBitsArray
  echo (-7.int16).toBitsArray
  echo (7.int16).toBitsArray
    a = 0000000000000000000000000000000000000000000000000000000000000000000000
    b = 0000000000000000000000000000000000000000000000000000000000000000000000
set bits ...
set [0,1,2,3,4,69]-th bit of a
    a = 1111100000000000000000000000000000000000000000000000000000000000000001
set [6,7,8,9,65]-th bit of a
    b = 0000001111000000000000000000000000000000000000000000000000000000010000
a & b = 0000000000000000000000000000000000000000000000000000000000000000000000
a | b = 1111101111000000000000000000000000000000000000000000000000000000010001
a ^ b = 1111101111000000000000000000000000000000000000000000000000000000010001
   ~a = 0000011111111111111111111111111111111111111111111111111111111111111110
a.shl(1) =1111000000000000000000000000000000000000000000000000000000000000000010
a.shl(2) =1110000000000000000000000000000000000000000000000000000000000000000100
a.shl(3) =1100000000000000000000000000000000000000000000000000000000000000001000
a.shl(69)=1000000000000000000000000000000000000000000000000000000000000000000000
a.shr(1) =0111110000000000000000000000000000000000000000000000000000000000000000
a.shr(2) =0011111000000000000000000000000000000000000000000000000000000000000000
a.shr(3) =0001111100000000000000000000000000000000000000000000000000000000000000
a.shr(69)=0000000000000000000000000000000000000000000000000000000000000000000001
expand a to 100 bits: 1111100000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000
127.uint32.toBitsArray = 11111110000000000000000000000000
127.uint32.toBitsArray.reverseBits = 00000000000000000000000001111111
127.uint32.toBitsArray.reverseBits.reverseBits = 11111110000000000000000000000000
(uint32.high-3).toBitsArray = 00111111111111111111111111111111
(uint32.high-3).toBitsArray.reverseBits = 11111111111111111111111111111100