common/quvi/: Change license header
[libquvi-scripts.git] / share / common / quvi / bit.lua
blob6cdbfdf4436d4090ebd1aae6d261a7f5b45387fd
1 -- libquvi-scripts
2 -- Copyright (C) 2010 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local M = {}
23 function M.bit_or(x, y) -- http://is.gd/iVg4x
24 local p = 1
25 while p < x do p = p + p end
26 while p < y do p = p + p end
27 local z = 0
28 repeat
29 if p <= x or p <= y then
30 z = z + p
31 if p <= x then x = x - p end
32 if p <= y then y = y - p end
33 end
34 p = p * 0.5
35 until p < 1
36 return z
37 end
39 -- 1-based indexing
40 function M.bit(p) return 2 ^ (p - 1) end
42 -- e.g. "if has_bit (foo, bit (n)) then ..."
43 function M.has_bit(x, p) return x % (p + p) >= p end
45 return M
47 -- vim: set ts=2 sw=2 tw=72 expandtab: