Lazarus

Free Pascal => Beginners => Topic started by: Jake012345 on May 21, 2020, 06:25:45 pm

Title: Command exists?
Post by: Jake012345 on May 21, 2020, 06:25:45 pm
Hello!

Is there any command for this:

Code: Pascal  [Select][+][-]
  1.     if started=true then started:=false;
  2.     if started=false then started:=true;
  3.  

I tried the inc(); but it gave RunError...
Title: Re: Command exists?
Post by: balazsszekely on May 21, 2020, 06:28:04 pm
Try this:
Code: Pascal  [Select][+][-]
  1. Started := not Started;
Title: Re: Command exists?
Post by: Jake012345 on May 21, 2020, 06:29:36 pm
thanks!
Title: Re: Command exists?
Post by: MarkMLl on May 21, 2020, 08:34:30 pm
Code: Pascal  [Select][+][-]
  1.     if started=true then started:=false;
  2.     if started=false then started:=true;
  3.  

Assuming that, since you've shown it as valid Pascal syntax, that's an attempt at working code, then

started := true;

Now, noting the other (probably more helpful :-) answer you've been given, consider what you really asked:

Code: Pascal  [Select][+][-]
  1.     if started=true then started:=false;
  2.  
  3. // If started was true, it's now false.
  4. // If started was false, it's still false.
  5. // Hence in all cases: started is now false.
  6.  
  7.     if started=false then started:=true;
  8.  
  9. // We know that started was false, hence it must become true.
  10.  

What you probably meant to ask was

Code: Pascal  [Select][+][-]
  1.     if started = true then
  2.         started := false
  3.     else
  4.         if started = false then
  5.             started:=true;
  6.  

But if the  else  is taken started must be false therefore the second test is redundant, hence

Code: Pascal  [Select][+][-]
  1.     if started = true then
  2.         started := false
  3.     else
  4.         started := true;
  5.  

Hence, assuming that that was what you meant,

Code: Pascal  [Select][+][-]
  1.     started := not started;
  2.  

That's the problem with computers: they'll do exactly what you tell them, even if it's not what you mean :-)

MarkMLl
Title: Re: Command exists?
Post by: jamie on May 21, 2020, 08:56:52 pm
Hello!

Is there any command for this:

Code: Pascal  [Select][+][-]
  1.     if started=true then started:=false;
  2.     if started=false then started:=true;
  3.  

I tried the inc(); but it gave RunError...
The inc method would of worked if thought out a little differently

Inc(Byte(Start)); Start := Boolean( Byte(Start)and 1);

Nice sloppy code, just what I like..  8)
Title: Re: Command exists?
Post by: Ñuño_Martínez on May 23, 2020, 11:15:38 am
Start := Boolean( Byte(Start)and 1);
Obfuscation for the win! ;D
Title: Re: Command exists?
Post by: lucamar on May 23, 2020, 11:19:49 am
If you really want obfuscation this is a little "better":
Code: Pascal  [Select][+][-]
  1. Start := Boolean(Byte(Start) and Ord(True));
:D
Title: Re: Command exists?
Post by: ccrause on May 23, 2020, 03:00:53 pm
A more "simplified" version of GetMem's one line solution:
Code: Pascal  [Select][+][-]
  1.   Start := boolean((byte(Start) and ord(true)) xor ord(true));
Title: Re: Command exists?
Post by: MarkMLl on May 23, 2020, 03:12:25 pm
A more "simplified" version of GetMem's one line solution:

This is all great fun, but I certainly hope that the OP- as an admitted beginner- doesn't think that it is in any way acceptable practice.

MarkMLl
Title: Re: Command exists?
Post by: Kays on May 23, 2020, 03:39:42 pm
[…] I certainly hope that the OP […] doesn't think that it is in any way acceptable practice. […]
Yeah, agreed. Especially – examining the assembler output – it produces “slow” and “bulky” code. I think, we need to optimize it using assembly language:
Code: Pascal  [Select][+][-]
  1.         {$asmMode intel}
  2.         asm
  3.                 xor started, 1
  4.         end;
%)  :P
Title: Re: Command exists?
Post by: jamie on May 23, 2020, 04:20:56 pm
[…] I certainly hope that the OP […] doesn't think that it is in any way acceptable practice. […]
Yeah, agreed. Especially – examining the assembler output – it produces “slow” and “bulky” code. I think, we need to optimize it using assembly language:
Code: Pascal  [Select][+][-]
  1.         {$asmMode intel}
  2.         asm
  3.                 xor started, 1
  4.         end;
%)  :P

I could be wrong but I believe that is what the compiler does , if it does not it should be doing it !  %)
Title: Re: Command exists?
Post by: Thaddy on May 23, 2020, 04:38:39 pm
I would write it in db notation.
Title: Re: Command exists?
Post by: MarkMLl on May 23, 2020, 05:38:07 pm
What the compiler does behind closed doors is the business of nobody but the compiler and its consenting libraries.

However unless Ord(Succ(false)) is defined by the language to be 1 I'd be very uneasy about any negation based on Inc() or xor: it's almost universally accepted that false is represented by zero but in some languages Ord(not false) is -1 rather than 1.

In short, if you want to negate a boolean you use the not operator.

MarkMLl



Title: Re: Command exists?
Post by: marcov on May 23, 2020, 06:56:39 pm
Moreover there is a decent intrinsic for  x and 1:  odd()
Title: Re: Command exists?
Post by: PascalDragon on May 23, 2020, 09:43:10 pm
What the compiler does behind closed doors is the business of nobody but the compiler and its consenting libraries.

However unless Ord(Succ(false)) is defined by the language to be 1 I'd be very uneasy about any negation based on Inc() or xor: it's almost universally accepted that false is represented by zero but in some languages Ord(not false) is -1 rather than 1.

For (Object) Pascal the types Boolean, Boolean8, Boolean16, Boolean32 and Boolean64 use 0 for False and 1 for True while the types ByteBool, WordBool, LongBool and QWordBool use 0 for False and "not 0" (or -1) for True.
Title: Re: Command exists?
Post by: MarkMLl on May 23, 2020, 10:09:49 pm
What the compiler does behind closed doors is the business of nobody but the compiler and its consenting libraries.

However unless Ord(Succ(false)) is defined by the language to be 1 I'd be very uneasy about any negation based on Inc() or xor: it's almost universally accepted that false is represented by zero but in some languages Ord(not false) is -1 rather than 1.

For (Object) Pascal the types Boolean, Boolean8, Boolean16, Boolean32 and Boolean64 use 0 for False and 1 for True while the types ByteBool, WordBool, LongBool and QWordBool use 0 for False and "not 0" (or -1) for True.

It's been a long day and I'm not even going to start speculating on how that could be abused :-)

MarkMLl
Title: Re: Command exists?
Post by: ASerge on May 25, 2020, 03:14:42 pm
while the types ByteBool, WordBool, LongBool and QWordBool use 0 for False and "not 0" (or -1) for True.
Slightly more precisely, "all but zero", i.e. 1, 2, 100 and -100 also give True.
TinyPortal © 2005-2018