-
Notifications
You must be signed in to change notification settings - Fork 19
/
BitUnit.pas
118 lines (92 loc) · 3.38 KB
/
BitUnit.pas
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{**********************************************************************}
{* *}
{* Bit Unit, a unit for easy bit manipulation *}
{* Author: *}
{* Theodoros Bebekis *}
{* Thessaloniki, Greece *}
{* [email protected] *}
{* *}
{* Delfi's 2003 update *}
{* 3 new functions: *}
{* setbit, getbit, bitcopy *}
{* [email protected] *}
{* *}
{**********************************************************************}
unit BitUnit;
interface
function IsBitSet(const i, Nth: integer): boolean;
function BitToOn(const i, Nth: integer): integer;
function BitToOff(const i, Nth: integer): integer;
function BitToggle(const i, Nth: integer): integer;
function ReverseAllBits(const i: integer): integer;
// new
function bitcopy(source, dest, start, count, deststart: integer): integer;
function getbit(source, index: integer): boolean;
function setbit(source, index: integer; to_: boolean): integer;
implementation
// IsBitSet
// returns True if a bit is ON (1)
// Nth can have any bit order value in [0..31]
function IsBitSet(const i, Nth: integer): boolean;
begin
Result:= (i and (1 shl Nth)) <> 0;
end;
// BitToOn
// sets a bit in number to on and returns new number
function BitToOn(const i, Nth: integer): integer;
begin
if not IsBitSet(i, Nth)
then Result := i or (1 shl Nth) else Result:=i;
end;
// BitToOff
// sets a bit in number to off and returns new number
function BitToOff(const i, Nth: integer): integer;
begin
if IsBitSet(i, Nth)
then Result := i and ((1 shl Nth) xor $FFFFFFFF)
else Result:=i;
end;
// BitToggle
// toggles the state of a bit
function BitToggle(const i, Nth: integer): integer;
begin
Result := i xor (1 shl Nth);
end;
// ReverseAllBits
// reverses all bits (all zeroes to ones and ones to zeroes)
function ReverseAllBits(const i: integer): integer;
var N:integer;
begin
Result:= i;
for N:=0 to 31 do Result:= Result xor (1 shl N);
end;
// Added Delfi's functions
// setbit
//
function setbit(source, index: integer; to_: boolean): integer;
begin
case to_ of
true: result:= bittoon(source, index);
false: result:= bittooff(source, index);
end;
end;
// getbit
// same as isbitset with a name that makes more sense in the code where you use it.
function getbit(source, index: integer): boolean;
begin
Result:= (source and (1 shl index)) <> 0;
end;
// bitcopy
// copies a range of bits from source to dest
function bitcopy(source, dest, start, count, deststart: integer): integer;
var
N: integer;
begin
result:= dest;
for n:= start to start + count do begin
dest:= setbit(dest, deststart + n, getbit(source, n));
// if isbitset(source, n) then dest:= bittoon(dest, deststart + n) else dest:= bittooff(dest, deststart + n);
end;
result:= dest;
end;
end.