Recent

Author Topic: How to force the not operator to return a byte  (Read 9860 times)

Roland57

  • Full Member
  • ***
  • Posts: 187
How to force the not operator to return a byte
« on: May 14, 2021, 05:51:07 pm »
Hello!

I have the following code:

Code: Pascal  [Select][+][-]
  1. const
  2.   a = 128;
  3.   b =  64;
  4.   c = byte(not(a or b));
  5.  
  6. begin
  7. end.

With FPC 3.2.0, I get this warning; "Warning: range check error while evaluating constants (-193 must be between 0 and 255)"

(With FPC 3.0.4, I didn't get any warning. I don't know why.)

How could I get rid of the warning? I tried several things but didn't find the solution.

Regards.

Roland

winni

  • Hero Member
  • *****
  • Posts: 2478
Re: How to force the not operator to return a byte
« Reply #1 on: May 14, 2021, 06:04:15 pm »
Hi!

If you want to get a correct result and no warning do this:

Code: Pascal  [Select][+][-]
  1.     const
  2.       a = byte(128);
  3.       b =  byte(64);
  4.       c = byte(not(a or b));
  5.      
  6.     begin
  7.     end.

Cast both const to a byte to the range of 0..255. Const b is now only in the range 0..64 and so only 7 bits while  128 is 8 bits..

Winni

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
Re: How to force the not operator to return a byte
« Reply #2 on: May 14, 2021, 06:08:09 pm »
Try

Code: Pascal  [Select][+][-]
  1.     const
  2.       a: byte= 128;
  3.       b: byte=  64;
  4.       c { : byte } = not(a or b);
  5.      
  6.     begin
  7.     end.
  8.  

If that doesn't work try byte(a) etc., i.e. to make it absolutely explicit that you're not expecting it to be extended.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

howardpc

  • Hero Member
  • *****
  • Posts: 3725
Re: How to force the not operator to return a byte
« Reply #3 on: May 14, 2021, 06:09:28 pm »
try
Code: Pascal  [Select][+][-]
  1. const
  2.   a = ShortInt(128);
  3.   b = 64;
  4.   c = Byte(not(a or b));

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: How to force the not operator to return a byte
« Reply #4 on: May 14, 2021, 07:04:15 pm »
Thank you all for your answers.

@howardpc

Your proposition seems to solve the problem, but I don't understand it.  :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
Re: How to force the not operator to return a byte
« Reply #5 on: May 14, 2021, 07:19:16 pm »
Your proposition seems to solve the problem, but I don't understand it.  :)

Neither do I, since according to https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1 the largest ShortInt is 127.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4135
Re: How to force the not operator to return a byte
« Reply #6 on: May 14, 2021, 07:45:12 pm »
Your proposition seems to solve the problem, but I don't understand it.  :)
Neither do I, since according to https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1 the largest ShortInt is 127.

The quid of the question is that ShortInt forces the constant to be interpreted as/converted to a signed byte. To be fair, though, casting to Byte should work equally well (or better).
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.

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: How to force the not operator to return a byte
« Reply #7 on: May 14, 2021, 08:18:10 pm »
To be fair, though, casting to Byte should work equally well (or better).

Should, maybe, but doesn't.  :)

Code: Pascal  [Select][+][-]
  1. const
  2.   a = byte(128);
  3.   b = 64;
  4.   c = byte(not(a or b));
  5.  
  6. begin
  7. end.

Quote
Warning: range check error while evaluating constants (-193 must be between 0 and 255)

winni

  • Hero Member
  • *****
  • Posts: 2478
Re: How to force the not operator to return a byte
« Reply #8 on: May 14, 2021, 08:46:33 pm »
Hi!

Solution is in reply #1 above.

Code: Pascal  [Select][+][-]
  1. b = byte(64);

Winni

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: How to force the not operator to return a byte
« Reply #9 on: May 14, 2021, 09:25:33 pm »
Solution is in reply #1 above.

Thanks, but...

Quote
Warning: range check error while evaluating constants (-193 must be between 0 and 255)

 :-\

winni

  • Hero Member
  • *****
  • Posts: 2478
Re: How to force the not operator to return a byte
« Reply #10 on: May 14, 2021, 10:06:28 pm »
Hi!

Another reason to turn warnings off:

Code: Pascal  [Select][+][-]
  1. Unit HelterSkelter;
  2. {$warnings off}
  3.  
Winni

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: How to force the not operator to return a byte
« Reply #11 on: May 14, 2021, 10:13:17 pm »
Another reason to turn warnings off:

Yes, this is a also a solution.  :)

tetrastes

  • Full Member
  • ***
  • Posts: 119
Re: How to force the not operator to return a byte
« Reply #12 on: May 14, 2021, 10:35:28 pm »
Your proposition seems to solve the problem, but I don't understand it.  :)

Neither do I, since according to https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1 the largest ShortInt is 127.

MarkMLl

ShortInt(128) = -128
and
Code: Pascal  [Select][+][-]
  1. const
  2.   a = -128;
  3.   b = 64;
  4.   c = not(a or b);
  5.  
  6. begin
  7.   writeln(c);
  8. end.  
outputs 63.
While
Code: Pascal  [Select][+][-]
  1. const
  2.   a = 128;
  3.   b = 64;
  4.   c = not(a or b);
  5.  
  6. begin
  7.   writeln(c);
  8. end.  
outputs -193.
Why FPC 3.2.0 gives warning at explicit casting -193 to byte, is another question... ;)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
Re: How to force the not operator to return a byte
« Reply #13 on: May 14, 2021, 10:59:40 pm »
Why FPC 3.2.0 gives warning at explicit casting -193 to byte, is another question... ;)

In retrospect, I rather wish I'd not got involved in this :-)

From playing around a few minutes ago, I suspect that the bitwise operators aren't as well implemented at compilation time as we're assuming:

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. const
  6.   a: byte= 128;
  7.   b: byte= 64;
  8.   c= { not } a or b; <== test.pas(8,20) Error: Illegal expression
  9.  
  10. begin
  11. end.
  12.  

My first thought was that {$WRITEABLECONST OFF}  might help, but no such luck.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 2478
Re: How to force the not operator to return a byte
« Reply #14 on: May 14, 2021, 11:10:24 pm »
Hi!

Yes - it is getting stranger and stranger.

If you cast them in the const declaration as byte(64) the compiler does not complain. And gives the correct result.

Oh Lord, don't ask me questions! (© Graham Parker)

Winni

 

TinyPortal © 2005-2018