tag:blogger.com,1999:blog-29331675.post5888225277396875892..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: CompareValue for booleansgabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-29331675.post-66887391167813107912021-07-18T08:52:25.403+02:002021-07-18T08:52:25.403+02:00I know I'm a bit late to the game, but:
funct...I know I'm a bit late to the game, but:<br /><br />function CompareBoolean(const ALeft, ARight: boolean): integer;<br />const<br /> CComparisonMap: array[boolean, boolean] of integer = ((0, -1), (1, 0));<br />begin<br /> result := CComparisonMap[ALeft, ARight];<br />end;<br /><br />N@N@https://www.blogger.com/profile/00182898358621023955noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-27837764140950493312019-10-03T14:42:58.517+02:002019-10-03T14:42:58.517+02:00Or maybe even
Result := Ord(Ord(left)<>0) -...Or maybe even<br /><br />Result := Ord(Ord(left)<>0) - Ord(Ord(right)<>0)<br /><br />In case Delphi ever decides to start optimizing "left <> false" to simply "left".himselfvhttps://www.blogger.com/profile/16017177372896810459noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-77382039781861812002019-10-03T14:34:22.093+02:002019-10-03T14:34:22.093+02:00@Arioch:
sign() requires math.pas and will end up...@Arioch:<br /><br />sign() requires math.pas and will end up slow. And <0 is true in boolean!<br /><br />Result := Ord(left <> false) - Ord(right <> false);<br /><br />This also gives you the simplest assembly, better than "ifs" even.himselfvhttps://www.blogger.com/profile/16017177372896810459noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-27369843431207493622019-10-01T15:17:58.145+02:002019-10-01T15:17:58.145+02:00@Arioch, do the non-bit Boolean types cast down im...@Arioch, do the non-bit Boolean types cast down implicitly? I'm not sure.<br /><br />Your approach should work though. As long as casting to Cardinal doesn't cause problems for LongBools. Aren't LongBools signed?Tonyhttps://www.blogger.com/profile/03545078413296313429noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-1441453131419994632019-10-01T10:52:54.502+02:002019-10-01T10:52:54.502+02:00Funny thing, Intel also makes True being +1, in ha...Funny thing, Intel also makes True being +1, in hardware (see SETcc opcodes introduced into 80386 processor in early 1990-s). Dunno why, for -1 always seemed more natural TRUE to me, being the complement thus unifying bitwise and logic operations.Arioch, thehttps://www.blogger.com/profile/05506275753418154075noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-83653660378972744582019-10-01T10:48:32.876+02:002019-10-01T10:48:32.876+02:00You missed that value might come from outside. Lik...You missed that value might come from outside. Like in Microsoft compilers TRUE is -1 not +1 as in Borland compilers. Also, there can be any garbage like 42. Or -42.<br /> <br />Also, true(10) and true(100), which is larger?<br /> <br />So, my approach would be Result := Sign(cardinal(Left)) - Sign(Cardinal(Right);<br />Why not byte(...) ? Because there are also LongBool and WordBool data types.<br /><br />Or equivalent assembler code if we could have inline assembler functions :-DArioch, thehttps://www.blogger.com/profile/05506275753418154075noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-7148087450509417032019-09-30T23:45:24.271+02:002019-09-30T23:45:24.271+02:00Certainly better than my solution. I never claimed...Certainly better than my solution. I never claimed I have published the best one ;)gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-35417191040504928562019-09-30T23:44:37.945+02:002019-09-30T23:44:37.945+02:00For fun, of course!For fun, of course!gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-32106989625089851022019-09-30T22:00:17.145+02:002019-09-30T22:00:17.145+02:00Um, why not just do:
Result := Ord(Left) - Ord(Ri...Um, why not just do:<br /><br />Result := Ord(Left) - Ord(Right);<br /><br />For Booolean, False is 0 and True is 1, unless I've missed something.Tonyhttps://www.blogger.com/profile/03545078413296313429noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-30992168327314981392019-09-30T21:42:44.393+02:002019-09-30T21:42:44.393+02:00> Write this function without any if statements...> Write this function without any if statements<br /><br />But why would you do that...?<br /><br />I'd bet that if you compare the generated assembly of the two functions, the first version is more efficient.Andershttps://www.blogger.com/profile/03794139975784440136noreply@blogger.com