Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IntToBin function.

0 views
Skip to first unread message

Waleed Omran

unread,
Jan 11, 2002, 6:46:42 PM1/11/02
to
Hi there,
I want to ask if anyone has write a function to convert integer to
binary string. I wrote the following function:

type
TBitsCount = 1..32;

function IntToBin(iValue: Cardinal; nBits: TBitsCount): string;
var
i: Integer;
begin
SetLength(Result, nBits);

if ((iValue shr (nBits-1)) shr 1 = 0) then
for i := nBits downto 1 do
Result[i] := Chr(((iValue shr (nBits-i)) and 1) + Ord('0'))
else
raise EConvertError.Create('The integer can''t fit in bits count.');
end;

Is there any suggestion to make it better?
--
For email replies, please remove
"nospam." from my email address.


Ndi

unread,
Jan 11, 2002, 7:53:44 PM1/11/02
to
Function HasBit (Value,bitmask:integer):boolean;
begin
result :=(value and bitmask=bitmask;
end;

Function Iif(Condition: Boolean, a,b:string);
begin
result := b;
if condition then result := a;
end;

Function IntegerToStringBit(Value:Integer):string;
var i:integer;
begin
for i := 0 to 31 do
result := result + iif(HasBit(Value,1 shl i,'0','1');
end;

I guess. It's not compiled.

--
Andrei "Ndi" Dobrin
NDI Group
Brainbench MVP for Windows 95
www.brainbench.com

"Waleed Omran" <wmo...@nospam.hotmail.com> wrote in message
news:3c3f798b_2@dnews...

jacob muntner

unread,
Jan 12, 2002, 2:50:50 AM1/12/02
to
{ Convert integer value to binary string,limited by Digits}
function IntToBin(Value: Cardinal; Digits: Integer): String;
var
S: String;
begin
S := '';
While Digits > 0 do
begin
if Odd(Value) then
S := '1' + S Else S := '0' + S;
Value := Value shr 1;
Digits := Digits - 1;
end;
Result := S;
end;


Waleed Omran <wmo...@nospam.hotmail.com> wrote in message
news:3c3f798b_2@dnews...

Andrew Rybenkov

unread,
Jan 12, 2002, 4:19:48 AM1/12/02
to
> { Convert integer value to binary string,limited by Digits}
> function IntToBin(Value: Cardinal; Digits: Integer): String;
> var
> S: String;
> begin
> S := '';
> While Digits > 0 do
> begin
> if Odd(Value) then
> S := '1' + S Else S := '0' + S;
> Value := Value shr 1;
> Digits := Digits - 1;
> end;
> Result := S;
> end;

{ Convert integer value to binary string,limited by Digits}

const
DigitChar: array[Boolean] of Char = ('0','1');


function IntToBin(Value: Cardinal; Digits: Integer): String;
var
S: String;
begin
S := '';
While Digits > 0 do
begin

S := DigitChar[Odd(Value)] + S;


Value := Value shr 1;
Digits := Digits - 1;
end;
Result := S;
end;

or (without DigitChar)

...
S := Char( Ord('0')+Integer(Odd(Value)) ) + S;
...

--
Andrew Rybenkov,
the programmer who walks by himself.


Kristofer Skaug

unread,
Jan 12, 2002, 7:02:34 AM1/12/02
to
"Waleed Omran" <wmo...@nospam.hotmail.com> wrote in message
news:3c3f798b_2@dnews...
>
> raise EConvertError.Create('The integer can''t fit in bits
count.');

why do you need to make this check? Why not just ensure that you always
specify enough digits to hold the value.

> Is there any suggestion to make it better?

depends what you mean by "better". In this case I choose to interpret
"Better" as :"Faster" in which case I suggest these:

// Helper const in IntToBinStr below:
const ALL_32_BIT_0 = '00000000000000000000000000000000';

//***********************************************************************
*******
// Function Name: IntToBinStr
// Input parameters: Value: 32-bit integer
// Digits: desired number of digits (1-32)
// Return value: binary string
// Side effects: ..
// Conditions: ..
// Description: Converts 32-bit integer to binary string
// Notes: Modified version in v.01.02.05; Performance increase
is
// about x2 for 2 digits, x10 for 32 digits!
// For 1 digit though it is 10% slower than old
version.
// Could be faster with precalculated table of powers
(as in
// ByteToBinStr below), but then variable digits is
difficult.
//***********************************************************************
*******
function IntToBinStr(Value,Digits:integer):string;
var tmp: shortstring;
counter,pow: integer;
begin
tmp:=ALL_32_BIT_0;
SetLength(tmp,Digits);
pow:=1 shl (Digits-1);
if (Value<>0) then
for counter:=0 to Digits-1 do
if (Value and (pow shr counter))<>0 then tmp[counter+1]:='1';
Result:=tmp;
end; // function IntToBinStr

I also have a fixed-width specialization ByteToBinStr for display of
8-bit bytes: it is very fast.
Apart from the fixed-digits savings it also uses a precalculated array of
the bit values against which you need to AND, saves a lot of shifting in
the loop. You can of course easily extend that to the 32-bit case if you
like.

// used in ByteToBinStr below:
const BytePowers: array[1..8] of byte = (128,64,32,16,8,4,2,1);

//***********************************************************************
*******
// Function Name: ByteToBinStr
// Input parameters: Value: 8-bit byte
// Return value: 8-digit binary string
// Side effects: ..
// Conditions: Uses const array BytePowers[] above
// Description: Converts byte to 8-digit binary string
// Notes: Special fixed-format adaptation of IntToBinStr
//***********************************************************************
*******
function ByteToBinStr(Value:byte):string;
var i: integer;
begin
Result:='00000000'; // "default" value
if (Value<>0) then
for i:=1 to 8 do
if (Value and BytePowers[i])<>0 then Result[i]:='1';
end; // function ByteToBinStr


Kristofer Skaug

unread,
Jan 12, 2002, 6:55:11 AM1/12/02
to
"jacob muntner" <jaco...@hotmail.co.il> wrote in message
news:3c3fe9b1_2@dnews...

> if Odd(Value) then
> S := '1' + S Else S := '0' + S;

Comment: Adding up strings char by char is a relatively CPU-expensive
process.
Of course you may not be interested in performance, but anyway.


Bruce Roberts

unread,
Jan 14, 2002, 12:15:59 AM1/14/02
to

"Waleed Omran" <wmo...@nospam.hotmail.com> wrote in message
news:3c3f798b_2@dnews...
> Hi there,
> I want to ask if anyone has write a function to convert integer to
> binary string. I wrote the following function:
>
> type
> TBitsCount = 1..32;
>
> function IntToBin(iValue: Cardinal; nBits: TBitsCount): string;
> var
> i: Integer;
> begin
> SetLength(Result, nBits);
>
> if ((iValue shr (nBits-1)) shr 1 = 0) then
> for i := nBits downto 1 do
> Result[i] := Chr(((iValue shr (nBits-i)) and 1) + Ord('0'))
> else
> raise EConvertError.Create('The integer can''t fit in bits count.');
> end;
>
> Is there any suggestion to make it better?

Better in what sense?

function IntToBin1A (iValue : Cardinal; nBits : tBitsCount) : string;

var i : integer;

begin
if (nBits < 32) and ((iValue shr nBits) > 0)
then raise EConvertError.Create ('The integer can''t fit in bits count.');
Result := StringOfChar ('0', nBits);


for i := nBits downto 1 do

inc (Result [i], ((iValue shr (nBits - i)) and 1));
end;

0 new messages