Page 1 of 1

Bitwise operations

Posted: Mon Nov 04, 2024 2:25 pm
by mestnyi
Should they behave the same way?

Code: Select all

; example 1
For a = 0 To 64
  i.i = (1<<a)
  Debug "i "+a+" "+ i +" "+Hex(i)
Next a

; example 2
For a = 0 To 64
  q.q = (1<<a)
  Debug "q "+a+" "+ q +" "+Hex(q)
Next a

Re: Bitwise operations

Posted: Mon Nov 04, 2024 3:09 pm
by NicTheQuick
Compiled as a 64 bit executable they both should behave the same.

Re: Bitwise operations

Posted: Mon Nov 04, 2024 5:12 pm
by mestnyi
that is, an integer in a 64-bit os will be like a quard and in a 32-bit os it will be like a long?
if so, why was it invented?

Re: Bitwise operations

Posted: Mon Nov 04, 2024 5:18 pm
by NicTheQuick
Yes, that's correct. I was invented so it can be easily used to store handles and pointers which always have the same size as the underlying architecture.

So if you write `handle = CreateWindow(#PB_Any, ...)` then `handle` should be an integer.

Re: Bitwise operations

Posted: Mon Nov 04, 2024 5:49 pm
by mestnyi
ok, but then is quard the same number in 32-bit and 64-bit?
I would check it myself, but so far there is nothing. :)

Re: Bitwise operations

Posted: Mon Nov 04, 2024 6:07 pm
by Quin
mestnyi wrote: Mon Nov 04, 2024 5:49 pm ok, but then is quard the same number in 32-bit and 64-bit?
I would check it myself, but so far there is nothing. :)
No, a quad is always 8 bytes. Directly from the documentation:
Long
.l
4 bytes
-2147483648 to +2147483647
Integer
.i
4 bytes (using 32-bit compiler)
-2147483648 to +2147483647
Integer
.i
8 bytes (using 64-bit compiler)
-9223372036854775808 to +9223372036854775807
Quad
.q
8 bytes
-9223372036854775808 to +9223372036854775807
;)

Re: Bitwise operations

Posted: Mon Nov 04, 2024 8:10 pm
by infratec
Have you ever looked in the Help about variables :?:

https://www.purebasic.com/documentation ... ables.html

Re: Bitwise operations

Posted: Mon Nov 04, 2024 9:22 pm
by mestnyi
infratec wrote: Mon Nov 04, 2024 8:10 pm Have you ever looked in the Help about variables :?:

https://www.purebasic.com/documentation ... ables.html
:) I looked in, of course.

Re: Bitwise operations

Posted: Tue Nov 12, 2024 6:04 pm
by Olli
An 'integer' variable is used to have a standard syntax :
- boolean use
- bitwise masking from 0 to 31

--> whatever the OS, it is same source syntax.

Now, however, we cannot do lots of other use.

1) A sign treated on X86, but not on X64 :

Code: Select all

Define.i a
a - a
a + 1
a << 31
a >> 31
Debug a
; (1 on X64, while -1 on X86)
2) A high DWORD treated on X64, but not on X86

Code: Select all

Define.i a
a - a
a + %1001
a << 40
a >> 40
Debug a
; (displays '9' on X64, versus '0' on X86)
To conclude : whatever 32 or 64 bits, use integers for numeric value between -2giga and +2giga. And use integers for 32-bits masks.

If you want to manage bigger values, force the 'quad' type.

Code: Select all

    x86 x64
L   32  32
Q   64  64
i   32  64