Lazarus

Free Pascal => Beginners => Topic started by: mb on July 01, 2018, 02:54:53 pm

Title: Bit-Operator not working ?
Post by: mb on July 01, 2018, 02:54:53 pm
Hi guys,

short but very funny problem (as I always have) ...  :(
Why is THIS not working ?

Code: Pascal  [Select][+][-]
  1. value:=$4D and $20;

value is of type "byte".
Using "+" operator works fine, with correct result, but bit-operator is not working ...  %)
I am always getting "0" as result, but it should be 32d (20h)
Title: Re: Bit-Operator not working ?
Post by: engkin on July 01, 2018, 02:57:08 pm
You most likely meant to use OR, not AND.
Title: Re: Bit-Operator not working ?
Post by: mb on July 01, 2018, 03:02:55 pm
Code: Pascal  [Select][+][-]
  1. value:=$4D or $20;

returns "109" as result.

I dont understand this, every book I have explains it like I used it, but its not workung for me.
Do I have to include some libary or something?
Title: Re: Bit-Operator not working ?
Post by: engkin on July 01, 2018, 03:11:12 pm
109 is $6D. What are you expecting?
Title: Re: Bit-Operator not working ?
Post by: Thaddy on July 01, 2018, 03:18:25 pm
@mb
Than your "book" must be more like a reactionary pamphlet:
And means leave in all set bits that are common to both. It does not mean addition.
Or means include the bits that are set in either one or the other.
Title: Re: Bit-Operator not working ?
Post by: Handoko on July 01, 2018, 03:18:44 pm
I used SpeedCruch calculator, it gives this results:

Quote
77 and 32 = 0
77 or 32 = 109

$4D = 77
$20 = 32

So, everything is working correctly.
;D

https://en.wikipedia.org/wiki/Bitwise_operation
Title: Re: Bit-Operator not working ?
Post by: mb on July 01, 2018, 03:23:08 pm
DAMN .... 1 bit ... your right! Too much heat here in my room today. I am very sorry, a typical mistake ... took me now about an hour of time.

I expected $40, but that wound be:
Code: Pascal  [Select][+][-]
  1. $4D and $40
of course ... sorry ... but thanks for your answer, that gave me the hint. Thank you !

Sorry guys ...

But it is a great community and I am happy I found Lazarus and this forum!
Title: Re: Bit-Operator not working ?
Post by: avra on July 01, 2018, 07:16:50 pm
Code: Pascal  [Select][+][-]
  1. value:=$4D and $20;

value is of type "byte".

If you just want to test a bit state then you could use BitHelpers from here: https://bitbucket.org/avra/bithelpers. Your code would look like this then:
Code: Pascal  [Select][+][-]
  1. if value.Bit[5] then
  2.   SomeString := value.Bit[5].ToTrueFalseString;
Title: Re: Bit-Operator not working ?
Post by: Handoko on July 01, 2018, 08:10:22 pm
If you just want to test a bit state then you could use BitHelpers from here: https://bitbucket.org/avra/bithelpers.

That's interesting. Thank you for sharing it.
TinyPortal © 2005-2018