/
bitwise.awk
72 lines (55 loc) · 1.39 KB
/
bitwise.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# taken from: https://github.com/ssmccoy/awkbot/blob/master/src/bitwise.awk
#
# A small collection of bitwise operations.
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 43) borrowed from FreeBSD's jail.c:
# <tag@cpan.org> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Scott S. McCoy
# -----------------------------------------------------------------------------
##
# >>
function bit_right (value, distance) {
return value / (2 ^ distance)
}
##
# <<
function bit_left (value, distance) {
return value * (2 ^ distance)
}
##
# xor
function bit_xor (a, b, r, i, ia, ib) {
r = 0
for (i = 0; i < 32; i++) {
c = int(2 ^ i)
ia = int(a / c) % 2
ib = int(b / c) % 2
r += (ia || ib) && (ia != ib) * c
}
return r
}
##
# &
function bit_and (a, b, r, i, c, ia, ib) {
r = 0
for (i = 0; i < 32; i++) {
c = 2 ^ i
ia = int(a / c) % 2
ib = int(b / c) % 2
r += (ia && ib) * c
}
return r
}
##
# |
function bit_or (a, b, r, i, ia, ib) {
r = 0
for (i = 0; i < 32; i++) {
c = 2 ^ i
ia = int(a / c) % 2
ib = int(b / c) % 2
r += (ia || ib) * c
}
return r
}