* * *

Author Topic: Bit-Operator not working ?  (Read 916 times)

mb

  • New member
  • *
  • Posts: 5
Bit-Operator not working ?
« 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)
« Last Edit: July 01, 2018, 02:59:42 pm by mb »

engkin

  • Hero Member
  • *****
  • Posts: 2116
Re: Bit-Operator not working ?
« Reply #1 on: July 01, 2018, 02:57:08 pm »
You most likely meant to use OR, not AND.

mb

  • New member
  • *
  • Posts: 5
Re: Bit-Operator not working ?
« Reply #2 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?
« Last Edit: July 01, 2018, 03:07:07 pm by mb »

engkin

  • Hero Member
  • *****
  • Posts: 2116
Re: Bit-Operator not working ?
« Reply #3 on: July 01, 2018, 03:11:12 pm »
109 is $6D. What are you expecting?

Thaddy

  • Hero Member
  • *****
  • Posts: 7136
Re: Bit-Operator not working ?
« Reply #4 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.
« Last Edit: July 01, 2018, 03:24:00 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

Handoko

  • Hero Member
  • *****
  • Posts: 2640
  • My goal: build my own game engine using Lazarus
Re: Bit-Operator not working ?
« Reply #5 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

mb

  • New member
  • *
  • Posts: 5
Re: Bit-Operator not working ?
« Reply #6 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!
« Last Edit: July 01, 2018, 03:33:30 pm by mb »

avra

  • Hero Member
  • *****
  • Posts: 1367
    • Additional info
Re: Bit-Operator not working ?
« Reply #7 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;
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Handoko

  • Hero Member
  • *****
  • Posts: 2640
  • My goal: build my own game engine using Lazarus
Re: Bit-Operator not working ?
« Reply #8 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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus