Recent

Author Topic: Command exists?  (Read 1118 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 260
  • 'The knowledge is the key.'
    • FPCProjectShare
Command exists?
« 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...
Check my ProjectShare site and share your content: FPCShare

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Command exists?
« Reply #1 on: May 21, 2020, 06:28:04 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. Started := not Started;

Jake012345

  • Sr. Member
  • ****
  • Posts: 260
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Command exists?
« Reply #2 on: May 21, 2020, 06:29:36 pm »
thanks!
Check my ProjectShare site and share your content: FPCShare

MarkMLl

  • Hero Member
  • *****
  • Posts: 1328
Re: Command exists?
« Reply #3 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jamie

  • Hero Member
  • *****
  • Posts: 3648
Re: Command exists?
« Reply #4 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)
The only true wisdom is knowing you know nothing

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1061
    • Burdjia
Re: Command exists?
« Reply #5 on: May 23, 2020, 11:15:38 am »
Start := Boolean( Byte(Start)and 1);
Obfuscation for the win! ;D
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

lucamar

  • Hero Member
  • *****
  • Posts: 3075
Re: Command exists?
« Reply #6 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
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ccrause

  • Sr. Member
  • ****
  • Posts: 313
Re: Command exists?
« Reply #7 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));

MarkMLl

  • Hero Member
  • *****
  • Posts: 1328
Re: Command exists?
« Reply #8 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Kays

  • Full Member
  • ***
  • Posts: 215
  • Whasup!?
    • KaiBurghardt.de
Re: Command exists?
« Reply #9 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
Yours Sincerely
Kai Burghardt

jamie

  • Hero Member
  • *****
  • Posts: 3648
Re: Command exists?
« Reply #10 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 !  %)
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Command exists?
« Reply #11 on: May 23, 2020, 04:38:39 pm »
I would write it in db notation.

MarkMLl

  • Hero Member
  • *****
  • Posts: 1328
Re: Command exists?
« Reply #12 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



Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8785
  • FPC developer.
Re: Command exists?
« Reply #13 on: May 23, 2020, 06:56:39 pm »
Moreover there is a decent intrinsic for  x and 1:  odd()

PascalDragon

  • Hero Member
  • *****
  • Posts: 2257
  • Compiler Developer
Re: Command exists?
« Reply #14 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.

 

TinyPortal © 2005-2018