Created
January 29, 2015 07:45
-
-
Save advancedxy/084874058204d7dc80d2 to your computer and use it in GitHub Desktop.
simple BitMap impl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class BitMap(val n: Int) { | |
private val bytes = Array.fill(n / 8 + 1)(0.toByte) | |
def setBit(i: Int) { | |
bytes(i / 8) = (bytes(i / 8) | (1 << (i & 7))).toByte | |
} | |
def unSetBit(i: Int) { | |
bytes(i / 8) = (bytes(i / 8) & ~(1 << (i & 7))).toByte | |
} | |
def getBit(i: Int): Boolean = { | |
(bytes(i / 8) & (1 << (i & 7))) > 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/scala/scala/blob/2.11.x/src/library/scala/collection/mutable/BitSet.scala
Scala std lib has BitSet!