oboy/src/core/bit.ml

21 lines
455 B
OCaml
Raw Normal View History

2016-02-02 21:25:53 +01:00
(**
* Copyright (c) 2015, Fabien Freling
* All rights reserved.
*
* This source code is licensed under the BSD 2-clause license found in the
* LICENSE file at the top level directory of this source tree.
*)
let is_set num i =
2015-04-19 19:18:20 +02:00
if i < 0 then
failwith "Invalid bit index."
else
(num lsr i) land 0b00000001 <> 0
2015-08-11 22:49:27 +02:00
let two_complement n =
(lnot n) + 1
let signed_byte n =
if n land 0x80 <> 0 then -((two_complement n) land 0xFF)
else n