Bitwise operations

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Bitwise operations

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Bitwise operations

Post by NicTheQuick »

Compiled as a 64 bit executable they both should behave the same.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Bitwise operations

Post 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?
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Bitwise operations

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Bitwise operations

Post 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. :)
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Bitwise operations

Post 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
;)
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Bitwise operations

Post by infratec »

Have you ever looked in the Help about variables :?:

https://www.purebasic.com/documentation ... ables.html
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Bitwise operations

Post 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.
Olli
Addict
Addict
Posts: 1266
Joined: Wed May 27, 2020 12:26 pm

Re: Bitwise operations

Post 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
Post Reply