astroyerman
asked on
Easy one
Hi, this is a two part question
1.- If i got three hex numbers how can i make a AND operation ex
uno := $21:
dos := $12:
tres := $1;
res = (uno AND dos AND tres);
how can i make this in delphi?
2.- How can i make an hex number transform into a binary number in a string?
Help, please.
Greeting
Yerman
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Btw, that was stolen from: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=3c40251a_1%40dnews&rnum=6
you already wrote right way to AND hex ;)(in delphi there is not such type hex numbers.you can write a constant as hex, but compiler treats it as integer (or other apropriate type if not specified)
only one note - res must be of numeric type (integer, double etc.)
function IntToBin(const Value: int64; const Digits: integer): string;
const
a01: array [0..1] of char = ('0', '1');
var
i, j, s: integer;
begin
j := 0;
s := SizeOf(int64)*8;
SetLength(Result, s);
for i := 0 to s do begin
Result[s - i +1] := a01[((Value shr i) and 1)];
if (Result[s - i +1] = '1') then j := 0
else if (j = 0) then j := s- i;
end;
if j > s-Digits then j := s-Digits;
Move(Result[j+2], Result[1], s-j);
SetLength(Result, s-j);
end;
wbr, mo.
only one note - res must be of numeric type (integer, double etc.)
function IntToBin(const Value: int64; const Digits: integer): string;
const
a01: array [0..1] of char = ('0', '1');
var
i, j, s: integer;
begin
j := 0;
s := SizeOf(int64)*8;
SetLength(Result, s);
for i := 0 to s do begin
Result[s - i +1] := a01[((Value shr i) and 1)];
if (Result[s - i +1] = '1') then j := 0
else if (j = 0) then j := s- i;
end;
if j > s-Digits then j := s-Digits;
Move(Result[j+2], Result[1], s-j);
SetLength(Result, s-j);
end;
wbr, mo.
With all the other help, I'll take my chance to show off my a fast 32 bit piece. The same Pascal code would apply to the 64 bit case, but it would compile to something very different.
One further obvious optimization would be to pull this down to an inline assembly function, taking advantage of the loopnz to decrement the index (ECX register) and handle the "while loop" functionality in one instruction, and taking advantage of the CPU string handling functions using the same ECX register. It could probably be doubled in speed one more time!
function IntToBinStr(Value:integer) :string;
// Value is contained in EAX so it will be very fast ...
var
i: Integer;
begin
result := '';
// Avoid string addition - define the string length immediately
SetLength(result,32);
i := 32;
while i > 0 do
begin
// - Fast dereference characters by treating the
// string as an array of char
// - Minimize computation by taking advantage of
// the result of the bitwise AND operator
// - chr (x) and ord (x) are pseudo-functions,
// so there is no overhead associated with them
// Their work is done at compile time
result [i] := chr (ord ('0')+(value and 1));
// Go to the next higher order bit
value := value shr 1;
// decrement the index
// this is 70ms faster per million calls than dec (x)
i := i-1;
end;
end; // function IntToBinStr
One further obvious optimization would be to pull this down to an inline assembly function, taking advantage of the loopnz to decrement the index (ECX register) and handle the "while loop" functionality in one instruction, and taking advantage of the CPU string handling functions using the same ECX register. It could probably be doubled in speed one more time!
function IntToBinStr(Value:integer)
// Value is contained in EAX so it will be very fast ...
var
i: Integer;
begin
result := '';
// Avoid string addition - define the string length immediately
SetLength(result,32);
i := 32;
while i > 0 do
begin
// - Fast dereference characters by treating the
// string as an array of char
// - Minimize computation by taking advantage of
// the result of the bitwise AND operator
// - chr (x) and ord (x) are pseudo-functions,
// so there is no overhead associated with them
// Their work is done at compile time
result [i] := chr (ord ('0')+(value and 1));
// Go to the next higher order bit
value := value shr 1;
// decrement the index
// this is 70ms faster per million calls than dec (x)
i := i-1;
end;
end; // function IntToBinStr
And yes, I did bother to measure the overhead of the inc(x)/dec(x) versus x := x+1 ... it made a 10% improvement in the performance of a core piece of my app.
2. Sounds like a homework assignment. We don't do homework assignments here.
At computation time, Delphi does not distinguish "hex" from "decimal". That is handled at compile time so all numbers are binary. To express a binary as a string you can take many approaches. The only operator to do this that you are missing from your question is the SHR (shift right)
res := uno shr 1
Between this and the "AND" operator you should have everything you need.