[fpc-pascal] Range check error warning.
Alexander Grotewohl
alex at dcclost.com
Tue Mar 24 17:06:30 CET 2020
Yes, those constants are defaulting to integer, which can represent both negative and positive values. When you do your shl you're inadvertently causing them to go into the range that pascal thinks is negative.
The typed constant should fix it.
--
Alexander Grotewohl
https://dcclost.com
________________________________
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> on behalf of wkitty42 at windstream.net <wkitty42 at windstream.net>
Sent: Tuesday, March 24, 2020 11:49:33 AM
To: fpc-pascal at lists.freepascal.org <fpc-pascal at lists.freepascal.org>
Subject: Re: [fpc-pascal] Range check error warning.
On 3/23/20 8:08 PM, fredvs via fpc-pascal wrote:
> const
> foldhiddenbit = 7;
> foldhiddenmask = 1 shl foldhiddenbit;
> currentfoldhiddenbit = 6;
> currentfoldhiddenmask = 1 shl currentfoldhiddenbit;
> foldlevelmask = byte(not (foldhiddenmask or currentfoldhiddenmask));
> -------->Here the warning
>
> I dont understand how the compiler can find -193 as value for
> foldlevelmask...
so let's work through it manually... with a little rambling because too much
c0ffee...
foldhiddenmask is 10000000 binary (128 decimal; 80 hex)
currentfoldhiddenmask is 01000000 binary (64 decimal; 40 hex)
ORing them gives 11000000 binary (192 decimal; c0 hex)
applying the NOT gives 00111111 binary (63 decimal; 3f hex)
so that seems to come out as expected but i found several online bitwise
calculators that came with the same -193 answer that fpc returned... this seems
to be related to a wrong field initialization of some kind or using the wrong
size for the value...
go here: http://easyonlineconverter.com/converters/bitwise-calculator.html
place 11000000 in field 1
leave field 2 blank
select "NOT"
click calculate
this one, https://toolslick.com/math/bitwise/not-calculator , gives the same
-193 result but you can also see that it is using 16 bits (word) instead of 8
bits (byte) so the top 8 zero bits are also NOTted which makes them ones and
there's the error... 1111111100111111 == -193... so i tried to trick it and used
16bits... damned thing prepended 8 more ones to the beginning... they're not
respecting that this is a byte we're working with...
i almost feel like telling both of them and the others i found that their
calculators are broken pretty badly... so back to your immediate problem...
apparently your byte() is not working as desired?
perhaps byte() is in the wrong place? perhaps it should be
foldlevelmask = not (byte(foldhiddenmask) or byte(currentfoldhiddenmask));
perhaps the top 8 bits need to be cleared by forcing them to zeros somehow if
the default size of a const is larger than a byte?
perhaps it should be specified that all four of foldhiddenbit, foldhiddenmask,
currentfoldhiddenbit, currentfoldhiddenmask are of byte instead of word or other?
const
foldhiddenbit : byte = 7;
foldhiddenmask : byte = 1 shl foldhiddenbit;
currentfoldhiddenbit : byte = 6;
currentfoldhiddenmask : byte = 1 shl currentfoldhiddenbit;
foldlevelmask : byte = not (foldhiddenmask or currentfoldhiddenmask);
*or*
foldlevelmask : byte = not (byte(foldhiddenmask) or byte(currentfoldhiddenmask));
NOTE: i do not currently have FPC (or lazarus) installed so i haven't tested
this in fpc... i needed some drive space a few weeks back and it was easiest to
remove fpc and lazarus at the time... especially since i had four or five
versions of each installed with full source code...
--
NOTE: No off-list assistance is given without prior approval.
*Please keep mailing list traffic on the list where it belongs!*
_______________________________________________
fpc-pascal maillist - fpc-pascal at lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20200324/2ca990a2/attachment-0001.html>
More information about the fpc-pascal
mailing list