Recent

Author Topic: Command exists?  (Read 2791 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
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...

balazsszekely

  • Guest
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: 270
  • Knowledge is the key
Re: Command exists?
« Reply #2 on: May 21, 2020, 06:29:36 pm »
thanks!

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 6090
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: 1186
    • 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: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ccrause

  • Hero Member
  • *****
  • Posts: 845
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: 6676
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Hero Member
  • *****
  • Posts: 569
  • 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: 6090
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: 14201
  • Probably until I exterminate Putin.
Re: Command exists?
« Reply #11 on: May 23, 2020, 04:38:39 pm »
I would write it in db notation.
Specialize a type, not a var.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
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



MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • 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: 5446
  • 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