Forum > Third party

BitHelpers - bit manipulation for standard pascal types

<< < (6/7) > >>

avra:
I am glad to announce that documentation and console example have been added to the latest FPC trunk.

https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/rtl-objpas/examples
https://gitlab.com/freepascal.org/fpc/documentation/-/blob/main/xml/syshelpers.xml
https://gitlab.com/freepascal.org/fpc/documentation/-/blob/main/xml/sysutils.xml

GUI example does not belong to FPC so it will remain available at https://gitlab.com/freepascal.org/fpc/source/-/issues/39541 as file attachment 20220120-3-examples.zip.

This concludes migration of bithelpers to FPC trunk. Of course, you may continue to shoot related questions here.

Enjoy!  :D

WayneSherman:
Thank you for your work on these and getting them merged into fpc.

The bit functions such as ClearBit, SetBit, and TestBit take bit index values, but they don't work with bit mask values.  It would be nice to add bit functions that work with masks as well since many APIs and libraries use them.  Something like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FLAG1 = $01;FLAG2 = $02;FLAG3 = $04;FLAG4 = $08;... MyFlags.ClearFlags(FLAG1 or FLAG3);MyFlags.SetFlags(FLAG2);if MyFlags.HasAnyFlags(FLAG1 or FLAG4) then...if MyFlags.HasAllFlags(FLAG1 or FLAG4) then...
File attributes on Windows are defined like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     FILE_ATTRIBUTE_READONLY             = $0000001;     FILE_ATTRIBUTE_HIDDEN               = $0000002;     FILE_ATTRIBUTE_SYSTEM               = $0000004;     FILE_ATTRIBUTE_DIRECTORY            = $0000010;     FILE_ATTRIBUTE_ARCHIVE              = $0000020;     FILE_ATTRIBUTE_DEVICE               = $0000040;     FILE_ATTRIBUTE_NORMAL               = $0000080; //so you could do thisif MyFileAttr.HasFlags(FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM) then
I recently had to do this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if GetConsoleMode(TextRec(Input).Handle, ConsoleMode) then begin    ConsoleMode := ConsoleMode and not ENABLE_QUICK_EDIT_MODE;    SetConsoleMode(TextRec(Input).Handle, ConsoleMode);  end;
But would have loved to do this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if GetConsoleMode(TextRec(Input).Handle, ConsoleMode) then       SetConsoleMode(TextRec(Input).Handle, ConsoleMode.ClearFlags(ENABLE_QUICK_EDIT_MODE));

WayneSherman:
I haven't decided if I like "Mask" or "Flag":


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---MyFlag.ClearMask(FLAG1);MyFlag.SetMask(FLAG3 or FLAG4);if MyFlags.TestMask(FLAG1 or FLAG3) then...if MyFlags.TestMaskAll(FLAG1 or FLAG3) then...if MyFlags.TestMaskAny(FLAG1 or FLAG3) then...
Also, we have a Clear operation, but I did not see a helper to set all bits:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---MyFlag.Clear;  //not a function, operates on the variable directlyMyFlag.SetAllBits;  //not a function, operates on the variable directly

440bx:

--- Quote from: WayneSherman on May 01, 2022, 09:52:42 am ---I haven't decided if I like "Mask" or "Flag":

--- End quote ---
I "vote" for "Mask"  because it's more descriptive than (not as generic as) "Flag" and also because it's consistent with the helpers' names.

WayneSherman:
or maybe add the word "Bits":


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    MyFlag.ClearMaskBits(FLAG1);    MyFlag.SetMaskBits(FLAG3 or FLAG4);    if MyFlags.TestMaskBits(FLAG1 or FLAG3) then...  //all bits must match for true    //is this even needed?     if MyFlags.TestMaskAny(FLAG1 or FLAG3) then...  //any bit must match for true
Maybe this was discussed, but the way helpers look, using a dot operator, leads one to believe the variable itself gets modified.  Should a helper like this be written as a function or procedure?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---MyFlag.ClearMaskBits(FLAG1);  //not a function, operate on the variable directly//versusMyFlag := MyFlag.ClearMaskBits(FLAG1);  //a function, MyFlag is not changed by the helper
For example, a record or object generally are expected to operate on their own data:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---MyStrRec.SetValue('hello world'); //would be expected to change the record variableMyObject.SetValue(True); //would be expected to modify data in the object.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version