Is there another way of writing
var iVar = 0x0ffff0000.int
just (with a suffix or so) like
var i32var = 0x0ffff0000i32
Fiddling with bit set variables from data protocols it would be nice to write something like
iVar == 0x0ffff0000
which bails out because the rhs becomes an int64 rather than an int. I could use
iVar == 0x0ffff0000i32
but this would be extended to 0xffffffff'ffff0000 when
int.sizeof == int64.sizeof
Any thoughts?
I found a solution which is semi elegant only but does the job. It was trickier (for me at least) than originally anticipated:
proc `!`(n: int64): int {.compileTime.} =
when int.sizeof == int64.sizeof:
(n and 0xffffffff).int
else:
if (n and 0x80000000) != 0:
(n or (not 0xffffffff)).int
else:
n.int
This allows me to write something like
proc tryLayers(v: int): MpgWhat {.inline.} =
block dontKnow:
case v and !0xfffe0000:
of !0xfffa0000, # MP3, M1A (layer III), v1
!0xfffc0000, # MP2, M1A (layer II), v1
# MPA, M1A starts with fffe => utf16le BOM
!0xfff20000, # MP3, M2A (layer III), v2
!0xfff40000, # MP2, M2A (layer II), v2
!0xfff60000, # MPA, M2A (layer I), v2
!0xffe20000: # MP3, M25A (layer III), v2.5
[...]
which works regardless of architecture for 32bit values loaded into int:
when int.sizeof == int64.sizeof:
doAssert !0xffffffffff == 0xffffffff
else:
doAssert !0xffffffff == -1
doAssert !0x7fffffff == int.high
@jordan, CMIIW, are you perhaps don't want to write it like this?
var i32var = 0xffff0000'i32
:)
yes I would like but prefer to work in integer and not provide two code versions for 64 and 32 bit architecture (unless I got something wrong)
the problem is the sign extension when i32 is propagated to 64 bit int, and u32 opens a can of worms (sign propagation is what I normally want but not here)