Re: bitwise operation doesn't work, what am I doing wrong?



MikyMike wrote:

Hi, I try to do a bitwise operation to see if Alignment containt
DT_LEFT, but it always return false. Anyone have an idea why this
doesn't work?


procedure TForm1.Button1Click(Sender: TObject);
Var Alignment : Cardinal;
begin
Alignment := DT_LEFT and DT_VCENTER;
If (Alignment And DT_LEFT) <> 0 Then Caption := 'yes' else Caption
:= 'no';
end;


assuming Both constants are not the same.
the Results will always be false.
i think what you're trying to do is set both flags.
try this.
Alignment := DT_LEFT Or DT_VCENTER.

with the "OR" you're setting the results to true if either
the Alignment current value has the bit set or the DT_VCENTER is set.
where is.
Alignment := DT_LEFT, then Alignment := Alignment or DT_VCENTER.
using the AND operator.
Both values in the same Bit location must be set before the results
is also set.
Basically, it means this.
One and the other Must match before the Results also matches.
seeing that DT_LEFT , DT_VCENTER most likely do not match, neither
one of them will be in the results. Or, if they have some bits in there
values that just happen to have the same values, only those bits will be
the results, how ever, the value most likely won't match either one.
for example
Value1 = $0F;
Value2 = $F0;
Results := Value1 and Value2; will = 0. because both values have bits turn on that do not match each other.
here is another scenario.
Value1 = $1F;
Value2 = $F0;
Results := Value1 and Value2; will = $10; etc..

The AND operator is not an operation like A+B where is, the +
would be the "AND"
it simple means for example, IF Bit1 of Value 1 is on and Bit 1
of Value 2 is on, then the results at that bit location would also be
on. All bits are evaluated this way.
with OR,
If Bit 1 in value 1 is on or, Bit 1 in value 2 is on, the results is, bit 1 is on.


hope that helped.


--
"I'm never wrong, once i thought i was, but was mistaken"
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5

.


Quantcast