Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Endian swapping in QB64pe
#8
(03-22-2025, 03:47 PM)tantalus Wrote: Thanks @SMcNeil, but I see this as not really acceptable in a language where I have explicitly declared the variable type, and I would have used OPTION _EXPLICIT also to make sure all my variables were correctly type declared to then have to use type suffixes too.

Too many unnecessary temporary variables also mixing unsigned and signed, although my code above uses 2, I only did that for clarity/sanity.  My original code was a function that worked on the reference and passed that back. Is this a quirk/niggle of QB64pe that will stay?
The problem is mixing the signed literals with the unsigned variables. `&HFF00` is a signed 16-bit integer, so it undergoes sign extension when AND'd with an unsigned 32-bit variable, turning it into `&HFFFFFF00`. If you specify the original as `&HFF00~%` (unsigned 16-bit integer) then the problem goes away since sign extension won't happen when the value gets promoted to 32-bit.

The other confusing aspect is that the number of digits you specify doesn't determine the type, so `&HFF00` and `&H0000FF00` are both signed 16-bit integers, adding extra zeros doesn't make it into a 32-bit integer. `&H0000FF00` is thus really `&HFFFFFF00` if you treat it as a 32-bit integer.

In other languages hex literals are usually unsigned so this doesn't come up when you use them, otherwise the rules regarding integer promotion and such are basically the same. As much as I'd like to change hex literals to be unsigned by default it can't be done without breaking a significant number of existing QBasic and QB64 programs so it's not happening. You can always add the type suffix to make the literal unsigned though.
Reply


Messages In This Thread
Endian swapping in QB64pe - by tantalus - 03-22-2025, 10:19 AM
RE: Endian swapping in QB64pe - by Petr - 03-22-2025, 02:54 PM
RE: Endian swapping in QB64pe - by tantalus - 03-22-2025, 03:10 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 03:25 PM
RE: Endian swapping in QB64pe - by tantalus - 03-22-2025, 03:47 PM
RE: Endian swapping in QB64pe - by DSMan195276 - 03-22-2025, 04:31 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 03:42 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 04:01 PM
RE: Endian swapping in QB64pe - by Petr - 03-22-2025, 05:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Swapping array elements PhilOfPerth 24 3,920 11-19-2022, 12:16 AM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)