Recent

Author Topic: Some fun with bitmanipulations  (Read 6886 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #30 on: October 14, 2019, 02:36:16 pm »
Well, you have helped a lot, thank you!! but I rewrote the architecture on Michael's suggestion...
( the byte /word /integer part I already figured out.. You need word btw, not byte, to handle ordinals on current processors that handle up to 512 (exotics even more) per register and are just not yet implemented, same as current 64)

Anyway, the discussion made me rethink some parts in a fundamental way.
Specialize a type, not a var.

Tz

  • Jr. Member
  • **
  • Posts: 54
  • Tz with FPC Pen Cil
Re: Some fun with bitmanipulations
« Reply #31 on: October 14, 2019, 04:00:40 pm »
What :o

this are more way seriously fun

Code: Pascal  [Select][+][-]
  1. program iso8583;  // https://en.wikipedia.org/wiki/ISO_8583
  2. {$mode objfpc}
  3. {$h+}
  4. uses SysUtils, OrdinalBits;
  5.  
  6. var
  7.   bitmap :QWord;
  8.   i :Integer;
  9.  
  10. begin
  11.  
  12.   bitmap := $2210001102C04804;
  13.   WriteLn(bitmap.ToHexString);
  14.  
  15.   for i:=63 downto 0 do
  16.     WriteLn('bit [',64-i,'] : ', bitmap.TestBit(i));
  17.  
  18. end.
  19.  

and toggle 512 exotic bulbs on or off ...

Awesome   :D :D :D

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #32 on: October 14, 2019, 04:14:18 pm »
Well, testbit won't help you very much but setbit/clearbit and togglebit do... It was designed with electronics in mind.
And yes, you can toggle a lot on or of, you can even MMAP an appropriate value to a hardware control like GPIO.
Specialize a type, not a var.

Tz

  • Jr. Member
  • **
  • Posts: 54
  • Tz with FPC Pen Cil
Re: Some fun with bitmanipulations
« Reply #33 on: October 14, 2019, 04:43:01 pm »
In ISO8583 TestBit is useful for determine existence of secondary bitmap. 
TestBit(1) in primary bitmap (or 63 in your helper) if true, there must be a secondary bitmap.

Master and Visa are serious and you make programming fun, :D
such ethos are less found today, in my place.

Excellent Thaddy,  and Thank you all for this forum,  so many ideas here.

« Last Edit: October 14, 2019, 04:46:27 pm by Tz »

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Some fun with bitmanipulations
« Reply #34 on: October 14, 2019, 05:29:05 pm »
In ISO8583 TestBit is useful for determine existence of secondary bitmap. 
TestBit(1) in primary bitmap (or 63 in your helper) if true, there must be a secondary bitmap.

Master and Visa are serious and you make programming fun, :D
such ethos are less found today, in my place.

Excellent Thaddy,  and Thank you all for this forum,  so many ideas here.


Yeah!!!

I know what you mean Tz, I once had to do it the hard way !!!

And managed everything in strings of '0' and '1' fearing problems with endianness.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Some fun with bitmanipulations
« Reply #35 on: October 14, 2019, 05:41:37 pm »
I haven't looked at the implementation of the bit setting code but I hope it's inline because I hate unused lard in my EXE 's  >:(
The only true wisdom is knowing you know nothing

Tz

  • Jr. Member
  • **
  • Posts: 54
  • Tz with FPC Pen Cil
Re: Some fun with bitmanipulations
« Reply #36 on: October 15, 2019, 03:20:15 pm »
garlar27  :D

couting finger may be? or check missing tooth?  :D

But this is real word protocol  https://en.wikipedia.org/wiki/Modbus

We can read or wite a coil,  and some enhancement we can read Octal, Nibble, Byte like SubBit(1, 2) = Value 0 - 3

example from digital input device 16 bit register

Name                    Min  Max   Bits/Sign
Unnamed digital input 1 0    15    13/16-16/16
Unnamed digital input 2 0    15    9/16-12/16
Unnamed digital input 3 0    15    5/16-8/16
Unnamed digital input 4 0    15    1/16-4/16


if Thaddy dont mind  :D


Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #37 on: October 15, 2019, 04:34:02 pm »
I haven't looked at the implementation of the bit setting code but I hope it's inline because I hate unused lard in my EXE 's  >:(
Of course it is inlined  :D And they are straight C function translations (Oops... :( ) from literature by Dennis Richie... O:-)
(True, but a bit kidding: it is very simple Boolean logic)
« Last Edit: October 15, 2019, 04:36:16 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #38 on: October 15, 2019, 04:39:24 pm »
@Tz
Yes, you can use that. I had hardware in mind.
If you know the memory hardware interface start address, you can fpmmap that address and use the helpers.
I  have a new version (on the bug tracker) that verifies if any of the operations actually succeeded.
Will add that here too.
Specialize a type, not a var.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Some fun with bitmanipulations
« Reply #39 on: October 15, 2019, 05:51:20 pm »
couting finger may be? or check missing tooth?  :D
Yeah !!! Something like that LOL  :D

I don't remember well exactly what I did since many years has passed from that day (I was using Laz 0.9.2 I think). I recall having trouble to convert the the received bitmap string and the BCD string to anything useful, so I did what I could (cast every char to byte then convert it to a binary string or something like that and being careful with the endiannes). Whatever I did back then worked well against the testing server but I wasn't happy....  :-[

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #40 on: October 15, 2019, 05:57:42 pm »
couting finger may be? or check missing tooth?  :D
Yeah !!! Something like that LOL  :D
Well the fingers were never an issue, but the teeth are now perfectly fine: I can even borrow them to you  if you need them %).
Specialize a type, not a var.

Tz

  • Jr. Member
  • **
  • Posts: 54
  • Tz with FPC Pen Cil
Re: Some fun with bitmanipulations
« Reply #41 on: November 17, 2019, 05:59:26 am »
@garlar27  I am using char >= '8'   %)    :)

@Thaddy thank you

how about BitSize, LSB, and MSB, so we can make pattern for disco bulbs   :D

for i := int.LSB to int.MSB do 
  process and or xor not bit


Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Some fun with bitmanipulations
« Reply #42 on: November 17, 2019, 08:35:16 am »
Well, the version in trunk has Ordinal bit index types, e.g. for byte TByteBitIndex etc.
And we already have Hi() Lo() intrinsics but MSB/LSB can be like so:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. // needs trunk
  3. uses sysutils;
  4. const
  5.   LSB_word = 0;
  6.   MSB_word = High(TWordBitIndex);
  7. var
  8.   bits:TWordBitIndex;
  9.   test:word = %1010101010101010;
  10. begin
  11.   writeln('%',test.ToBinString);
  12.   for Bits := LSB_word to MSB_word do test.ToggleBit(bits);
  13.   writeln('%',test.ToBinString);
  14. end.

A small example (for Arduino):
Code: Pascal  [Select][+][-]
  1. var
  2.   LEDPort: byte absolute PortA; // mapped hardware
  3.  
  4. const
  5.   LEDPin = 3;
  6. { old code from wiki
  7. implementation
  8.   LEDPort:= LEDPort or (1 shl LEDPin); //LED=on}
  9. { New code with trunk);}
  10.   LedPort.SetBit(LEDPin); // LED=on
See https://wiki.freepascal.org/AVR_Programming
The extensions were written with hardware in mind.
« Last Edit: November 17, 2019, 08:51:42 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018