diff --git a/Test/X2UtBitsTest.cfg b/Test/X2UtBitsTest.cfg new file mode 100644 index 0000000..fcec3dd --- /dev/null +++ b/Test/X2UtBitsTest.cfg @@ -0,0 +1,35 @@ +-$A8 +-$B- +-$C+ +-$D+ +-$E- +-$F- +-$G+ +-$H+ +-$I+ +-$J+ +-$K- +-$L+ +-$M- +-$N+ +-$O+ +-$P+ +-$Q- +-$R+ +-$S- +-$T- +-$U- +-$V+ +-$W- +-$X+ +-$YD +-$Z1 +-cg +-AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +-H+ +-W+ +-M +-$M16384,1048576 +-K$00400000 +-LE"c:\delphi6\Projects\Bpl" +-LN"c:\delphi6\Projects\Bpl" diff --git a/Test/X2UtBitsTest.dof b/Test/X2UtBitsTest.dof new file mode 100644 index 0000000..072d8f0 --- /dev/null +++ b/Test/X2UtBitsTest.dof @@ -0,0 +1,164 @@ +[FileVersion] +Version=6.0 +[Compiler] +A=8 +B=0 +C=1 +D=1 +E=0 +F=0 +G=1 +H=1 +I=1 +J=1 +K=0 +L=1 +M=0 +N=1 +O=1 +P=1 +Q=0 +R=1 +S=0 +T=0 +U=0 +V=1 +W=0 +X=1 +Y=1 +Z=1 +ShowHints=1 +ShowWarnings=1 +UnitAliases=WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; +[Linker] +MapFile=0 +OutputObjs=0 +ConsoleApp=1 +DebugInfo=0 +RemoteSymbols=0 +MinStackSize=16384 +MaxStackSize=1048576 +ImageBase=4194304 +ExeDescription= +[Directories] +OutputDir= +UnitOutputDir= +PackageDLLOutputDir= +PackageDCPOutputDir= +SearchPath= +Packages=vcl;rtl;dbrtl;vcldb;vclx;dss;dsnapcrba;dsnapcon;inetdb;webdsnap;websnap;dbxcds;Irc;parsdpk;hotspotter +Conditionals= +DebugSourceDirs= +UsePackages=0 +[Parameters] +RunParams=ip-to-country.csv countries.csv geo.db +HostApplication= +Launcher= +UseLauncher=0 +DebugCWD= +[Version Info] +IncludeVerInfo=1 +AutoIncBuild=0 +MajorVer=1 +MinorVer=0 +Release=0 +Build=0 +Debug=0 +PreRelease=0 +Special=0 +Private=0 +DLL=0 +Locale=1043 +CodePage=1252 +[Version Info Keys] +CompanyName= +FileDescription= +FileVersion=1.0.0.0 +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion=1.0.0.0 +Comments= +[Excluded Packages] +C:\Delphi6\Bin\dcldb60.bpl=Borland Database Components +C:\Program Files\madCollection\madBasic\Delphi 6\madHelp_.bpl=madHelp 1.1 · www.madshi.net +c:\delphi6\Projects\Bpl\X2CompsD6.bpl=X²Software Components - Designtime +c:\delphi6\Projects\Bpl\VirtualTreesD6D.bpl=Virtual Treeview +c:\delphi6\Bin\dcl31w60.bpl=Delphi 1.0 Compatibility Components +c:\delphi6\Bin\dclact60.bpl=Borland ActionBar Components +c:\delphi6\Bin\applet60.bpl=Borland Control Panel Applet Package +C:\Delphi6\Bin\dbx60.bpl=Borland SQL Explorer UI Package +C:\Delphi6\Projects\Bpl\tb2k_d6.bpl=Toolbar2000 Components (Jordan Russell) +c:\delphi6\Projects\Bpl\tb2kdsgn_d6.bpl=Toolbar2000 Design Package (Jordan Russell) +c:\delphi6\Bin\DCLNMF60.bpl=NetMasters Fastnet Tools +c:\delphi6\Bin\dclado60.bpl=Borland ADO DB Components +c:\delphi6\Bin\dclclxdb60.bpl=Borland CLX Database Components +C:\Delphi6\Bin\dclclxstd60.bpl=Borland CLX Standard Components +c:\delphi6\Bin\dclie60.bpl=Internet Explorer Components +C:\Delphi6\Projects\Bpl\aSQLitepkg.bpl=Aducom Software -- SQLite RunTime Components +c:\delphi6\Projects\Bpl\asqlite.bpl=Aducom Software -- SQLite Design Time Components +c:\delphi6\Projects\Bpl\PCtrlExd6.bpl=PageControlEx +c:\delphi6\Projects\Bpl\JvCoreD6D.bpl=JVCL Core Components +c:\delphi6\Projects\Bpl\JvSystemD6D.bpl=JVCL System Components +c:\delphi6\Projects\Bpl\JvStdCtrlsD6D.bpl=JVCL Standard Controls +c:\delphi6\Projects\Bpl\JvCtrlsD6D.bpl=JVCL Visual Controls +c:\delphi6\Projects\Bpl\JvCmpD6D.bpl=JVCL Non-Visual Components +c:\delphi6\Projects\Bpl\JvCustomD6D.bpl=JVCL Custom Controls +c:\delphi6\Projects\Bpl\JvDlgsD6D.bpl=JVCL Dialog Components +c:\delphi6\Projects\Bpl\JvCryptD6D.bpl=JVCL Encryption and Compression Components +c:\delphi6\Projects\Bpl\JvMMD6D.bpl=JVCL Multimedia and Image Components +c:\delphi6\Projects\Bpl\JvNetD6D.bpl=JVCL Network Components +c:\delphi6\Projects\Bpl\JvAppFrmD6D.bpl=JVCL Application and Form Components +c:\delphi6\Projects\Bpl\JvDBD6D.bpl=JVCL Database Components +c:\delphi6\Projects\Bpl\JvBDED6D.bpl=JVCL BDE Components +c:\delphi6\Projects\Bpl\JvInterpreterD6D.bpl=JVCL Interpreter Components +c:\delphi6\Projects\Bpl\JvBandsD6D.bpl=JVCL Band Objects +c:\delphi6\Projects\Bpl\JvPluginD6D.bpl=JVCL Plugin Components +c:\delphi6\Projects\Bpl\JvJansD6D.bpl=JVCL Jans Components +c:\delphi6\Projects\Bpl\JvGlobusD6D.bpl=JVCL Globus Components +c:\delphi6\Projects\Bpl\JvPrintPreviewD6D.bpl=JVCL Print Preview Components +c:\delphi6\Projects\Bpl\JvPageCompsD6D.bpl=JVCL Page Style Components +c:\delphi6\Projects\Bpl\JvValidatorsD6D.bpl=JVCL Validators and Error Provider Components +c:\delphi6\Projects\Bpl\JvUIBD6D.bpl=JVCL Unified Interbase Components +c:\delphi6\Projects\Bpl\JvWizardD6D.bpl=JVCL Wizard Design Time Package +c:\delphi6\Projects\Bpl\JvTimeFrameworkD6D.bpl=JVCL Time Framework +c:\delphi6\Projects\Bpl\JvHMID6D.bpl=JVCL HMI Controls design time unit +c:\delphi6\Projects\Bpl\JvManagedThreadsD6D.bpl=JVCL Managed Threads +c:\delphi6\Projects\Bpl\JvXPCtrlsD6D.bpl=JVCL XP Controls +c:\delphi6\Projects\Bpl\JvDockingD6D.bpl=JVCL Docking Components +c:\delphi6\Projects\Bpl\JvDotNetCtrlsD6D.bpl=JVCL DotNet Controls +c:\delphi6\Projects\Bpl\dclIndyCore60.bpl=Indy 10 Core Design Time +c:\delphi6\Projects\Bpl\dclIndyProtocols60.bpl=Indy 10 Protocols Design Time +c:\delphi6\Projects\Bpl\SysILS.bpl=System ImageList +c:\delphi6\Projects\Bpl\DragDropD6.bpl=Drag and Drop Component Suite +C:\Projects\Components\DevExpress\OrgChart Suite\Lib\dcldxOrgCD6.bpl=ExpressOrgChart by Developer Express Inc. +C:\Projects\Components\DevExpress\OrgChart Suite\Lib\dcldxDBOrD6.bpl=ExpressDBOrgChart by Developer Express Inc. +c:\delphi6\Projects\Bpl\BalloonD6.bpl=Balloon 2.0 +c:\delphi6\Projects\Bpl\DIPasDocD6.bpl=DiPasDoc - Designtime +C:\Delphi6\Projects\Bpl\DIContainers_D6.bpl=The Delphi Inspiration -- DIContainers +c:\delphi6\Bin\dclshlctrls60.bpl=Shell Control Property and Component Editors +c:\delphi6\Bin\dclsmp60.bpl=Borland Sample Components +c:\delphi6\Bin\dclbde60.bpl=Borland BDE DB Components +c:\delphi6\Bin\dclcds60.bpl=Borland Base Cached ClientDataset Component +C:\Delphi6\Bin\dclmid60.bpl=Borland MyBase DataAccess Components +c:\delphi6\Bin\dclbdecds60.bpl=Borland Local BDE ClientDataset Components +c:\delphi6\Bin\dclib60.bpl=InterBase Data Access Components +c:\delphi6\Bin\DBWEBXPRT.BPL=Borland Web Wizard Package +c:\delphi6\Bin\dcloffice2k60.bpl=Microsoft Office 2000 Sample Automation Server Wrapper Components +c:\delphi6\Bin\dcltee60.bpl=TeeChart Components +c:\delphi6\Bin\dcltqr60.bpl=TeeChart for QuickReport Components +c:\delphi6\Bin\dclnet60.bpl=Borland Internet Components +c:\delphi6\Bin\dclite60.bpl=Borland Integrated Translation Environment +c:\delphi6\Bin\dcldbx60.bpl=Borland dbExpress Components +c:\delphi6\Bin\dclsoap60.bpl=Borland SOAP Components +c:\delphi6\Bin\dclocx60.bpl=Borland Sample Imported ActiveX Controls +c:\delphi6\Bin\dcldbxcds60.bpl=Borland Local DBX ClientDataset Components +C:\WINDOWS\System32\ibevnt60.bpl=Borland Interbase Event Alerter Component +C:\Program Files\madCollection\madRemote\Delphi 6\madRemote_.bpl=madRemote 1.1b · www.madshi.net +C:\Program Files\madCollection\madKernel\Delphi 6\madKernel_.bpl=madKernel 1.3 · www.madshi.net +C:\Program Files\madCollection\madCodeHook\Delphi 6\madCodeHook_.bpl=madCodeHook 2.1b · www.madshi.net +C:\Program Files\madCollection\madSecurity\Delphi 6\madSecurity_.bpl=madSecurity 1.1n · www.madshi.net +C:\Program Files\madCollection\madShell\Delphi 6\madShell_.bpl=madShell 1.3k · www.madshi.net +c:\delphi6\Projects\Bpl\PsychoTidyD6.bpl=PsychoTidy IDE Expert diff --git a/Test/X2UtBitsTest.dpr b/Test/X2UtBitsTest.dpr new file mode 100644 index 0000000..b04282e --- /dev/null +++ b/Test/X2UtBitsTest.dpr @@ -0,0 +1,24 @@ +program X2UtBitsTest; + +{$APPTYPE CONSOLE} + +uses + X2UtBits; + +var + iTest: Integer; + bTest: T32Bits absolute iTest; + sTest: String; + +begin + Randomize(); + iTest := Random(MaxInt); + sTest := BitsToString(bTest, bs32); + + WriteLn('Value: ', iTest); + WriteLn('Bits: ', sTest); + + bTest := StringToBits(sTest); + WriteLn('Reversed: ', iTest); + ReadLn; +end. diff --git a/X2UtBits.pas b/X2UtBits.pas new file mode 100644 index 0000000..ea24cad --- /dev/null +++ b/X2UtBits.pas @@ -0,0 +1,130 @@ +{ + :: X2UtBits provides declarations and functions for easy bit manipulation + :: based on Delphi sets. + :: + :: Last changed: $Date$ + :: Revision: $Rev$ + :: Author: $Author$ +} +unit X2UtBits; + +interface +type + TBit = (bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7, + bit8, bit9, bit10, bit11, bit12, bit13, bit14, bit15, + bit16, bit17, bit18, bit19, bit20, bit21, bit22, bit23, + bit24, bit25, bit26, bit27, bit28, bit29, bit30, bit31, + bit32, bit33, bit34, bit35, bit36, bit37, bit38, bit39, + bit40, bit41, bit42, bit43, bit44, bit45, bit46, bit47, + bit48, bit49, bit50, bit51, bit52, bit53, bit54, bit55, + bit56, bit57, bit58, bit59, bit60, bit61, bit62, bit63); + + TBitSize = (bs8, bs16, bs24, bs32, bs64); + + T8Bit = bit0..bit7; + T16Bit = bit0..bit15; + T24Bit = bit0..bit23; + T32Bit = bit0..bit31; + T64Bit = bit0..bit63; + + T8Bits = set of T8Bit; + T16Bits = set of T16Bit; + T24Bits = set of T24Bit; + T32Bits = set of T32Bit; + T64Bits = set of T64Bit; + TMaxBits = set of TBit; + + TByteBits = T8Bits; + TWordBits = T16Bits; + TIntegerBits = T32Bits; + TInt64Bits = T64Bits; + + + // Unfortunately we can't really overload this (or at least I haven't found + // out how without an "Ambiguous overloaded call" error). + function BitsToString(const ABits: TMaxBits; const ASize: TBitSize): String; + + function StringToBits(const ABits: String): TMaxBits; + +implementation + +{======================================== + Bits to String +========================================} +function BitsToString; +var + bBit: TBit; + bMax: TBit; + iLength: Integer; + iPos: Integer; + +begin + case ASize of + bs8: + begin + bMax := bit7; + iLength := 8; + end; + bs16: + begin + bMax := bit15; + iLength := 16; + end; + bs24: + begin + bMax := bit23; + iLength := 24; + end; + bs32: + begin + bMax := bit31; + iLength := 32; + end; + bs64: + begin + bMax := bit63; + iLength := 64; + end; + end; + + SetLength(Result, iLength); + iPos := 1; + + for bBit := bMax downto bit0 do + begin + if bBit in ABits then + Result[iPos] := '1' + else + Result[iPos] := '0'; + + Inc(iPos); + end; +end; + + +{======================================== + String to Bits +========================================} +function StringToBits; +var + bBit: TBit; + iPos: Integer; + iLength: Integer; + +begin + Result := []; + bBit := bit0; + iLength := Length(ABits); + if iLength > 64 then + iLength := 64; + + for iPos := iLength downto 1 do + begin + if ABits[iPos] = '1' then + Include(Result, bBit); + + Inc(bBit); + end; +end; + +end.