VCL Delphi

Title: Using a ChecklistBox for bit manipulation
Question: Here is a easy way to display or manipulate all bit in a
32bit Integer/Cardinal
Answer:
Sometimes you want to see the bits in a Word in a convinient
representation.
Place a TCheckListBox component on your form.
Add 32 Items (in this order)
Bit 0
Bit 1
Bit 2
...
Bit 31
feel free to rename these items to give your bits a better
description.
use this procedure to set up the checklistbox:
//
procedure IntToCheckListBox(const Value:Cardinal; var clb:TCheckListBox);
var
i:Integer;
begin
for i:=0 to Max(clb.Items.Count-1, 31) do
begin
clb.Checked[i] := (Value and (1 shl i)) 0;
end;
end;
Call the following function in the 'OnClick' or 'OnClickCheck'
Event of the CheckListBox:
function CheckListBoxToInt(const clb:TCheckListBox):Cardinal;
var
i:Integer;
begin
Result := 0;
for i:=0 to Max(clb.Items.Count-1, 31) do
begin
if clb.Checked[i] then
Result := Result or (1 shl i);
end;
end;
Example:
procedure TForm1.CheckListBox1Click(Sender: TObject);
begin
MyValue := CheckListBoxToInt(Sender As TCheckListBox);
end;
Don't forget to add the 'Math' unit to your uses clause.
More than 32 bits ?
These procedures does the convertion.
procedure CheckListBoxToTBits(const clb:TCheckListBox; bits:TBits);
var
i:Integer;
begin
bits.Size := clb.Items.Count;
for i:=0 to clb.Items.Count-1 do
bits.Bits[i] := clb.Checked[i];
end;
procedure TBitsToCheckListBox(const clb:TCheckListBox; bits:TBits);
var
i:Integer;
begin
for i:=0 to Max(clb.Items.Count, bits.Size)-1 do
clb.Checked[i] := bits.Bits[i];
end;