Hello guys
Hello guys,
Is there binary literal support in Delphi?
I mean I can express numbers in binary way and it is resolved at compile time, something like this:
0b00000100 from C#7 and C++14
When I am still able to use hexadecimal representation, the binary way is a lot more representative when creating bitmasks and doing bitwise operations.
For example, when you want to extract GIF metadata, you will have to parse the Logical Screen Descriptor block, and it needs some bitwise operations to extract fields in the packed fields byte.
Instead of writing this:
$80 -> 1000 0000 ~> this mask will check if the file contains a global color table
$07 -> 0000 0111 ~> this mask will extract the size of the global color table
The binary representation is more aligned with the file documentation, since GIFs are binary files (this also apply to many other binary file formats), and looks much better to understand at the first time you see the code. I would love to see a way to write my masks in a binary way without needing to parse strings into binaries.
Thanks in advance :D
Is there binary literal support in Delphi?
I mean I can express numbers in binary way and it is resolved at compile time, something like this:
0b00000100 from C#7 and C++14
When I am still able to use hexadecimal representation, the binary way is a lot more representative when creating bitmasks and doing bitwise operations.
For example, when you want to extract GIF metadata, you will have to parse the Logical Screen Descriptor block, and it needs some bitwise operations to extract fields in the packed fields byte.
Instead of writing this:
$80 -> 1000 0000 ~> this mask will check if the file contains a global color table
$07 -> 0000 0111 ~> this mask will extract the size of the global color table
The binary representation is more aligned with the file documentation, since GIFs are binary files (this also apply to many other binary file formats), and looks much better to understand at the first time you see the code. I would love to see a way to write my masks in a binary way without needing to parse strings into binaries.
Thanks in advance :D
i know in FreePascal only: %01001 for example. Not works in Delphi :(
ReplyDeleteThere's no binary literal notation in Delphi. Normally, you would just use hexadecimal notation. Also have a look at TBits in Classes.
ReplyDeleteIt seems binary notation (for some reason?) is not supported. I do agree it would be nice for certain applications though.
ReplyDeleteMy only idea for readability is using constants:
const B00000000 = $0;
const B00000001 = $1;
... and so forth.
It's a pain to set up, but you'd only have to do it once.
I've always used hex constants, which are trivial to define, and built expressions using masks and SHL/SHR to extract subset values. I've never missed having bit tables at all.
ReplyDeleteadd this to QC
ReplyDeleteThen there is this for readability.
ReplyDeletetype
TColorTableHeader = type Byte;
TColorTableHeaderhelper = record helper for TColorTableHeader
private
function GetColorTableSize: Byte;
function GetHasColorTable: Boolean;
public
property HasColorTable:Boolean read GetHasColorTable;
property ColorTableSize:Byte read GetColorTableSize;
end;
function TColorTableHeaderhelper.GetColorTableSize: Byte;
begin
Result := Self and $07; // three lowest bits
end;
function TColorTableHeaderhelper.GetHasColorTable: Boolean;
begin
Result := (Self and $80) <> 0; // highest bits
end;
You can use StrToBin, if you just need to load the binary value once.
ReplyDeleteLars Fosdal How do you think in hexadecimal?
ReplyDeleteJacek Laskowski Binary literals aren't a new idea. C++14 has them, and GCC offered them in 2007. In fact, D has them, C# has them, Oxygene has them, Java has them, Python has them, Ruby has them, Rust has them, Perl has them.... The only language I can think of besides Delphi and C (although GCC offers it) that doesn't have binary literals is Go.
ReplyDeleteI think this means, sadly, that if they intended on implementing it in Delphi they probably would have done so already. :-(
Joseph Mitzen in 4 bit groups, why?
ReplyDeleteJoseph Mitzen
ReplyDelete1 = first bit, 2 = second, 4=3th, 8=4th. You just disassemble the hex digit into its elements 1,2,4,8 and reconstruct the binary image from that. Not that hard with a bit of practise.