Bit shift bug (not a bug)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Bit shift bug (not a bug)

Post by Olliv »

Code: Select all

X = 1 << 63
Y = X >> 1
Debug Hex(Y)
Should display 40000000 00000000, not
C0000000 00000000.
Last edited by Olliv on Wed Jun 21, 2017 11:56 am, edited 1 time in total.
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Bit shift bug

Post by DarkDragon »

Logic vs. arithmetic shift. Not a bug. The logic shift would behave as you want, but the arithmetic shift keeps the sign and fills it up.
bye,
Daniel
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Bit shift bug

Post by Olliv »

@Darkdragon

Thank you for answering.
Asm allows the coder to bypass this.
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Bit shift bug (not a bug)

Post by User_Russian »

This is a simple function that many years can not add in PB http://www.purebasic.fr/english/viewtop ... =3&t=52929
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Bit shift bug (not a bug)

Post by wilbert »

I requested it also before.
Both java and javascript have >> operator for arithmetic shift (like PB) and >>> for logical shift.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Bit shift bug (not a bug)

Post by Olliv »

@UserRussian and @Wilbert

Thank you for having recalled already posted infos. I move up UserRussian wish.
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Bit shift bug (not a bug)

Post by DarkDragon »

wilbert wrote:I requested it also before.
Both java and javascript have >> operator for arithmetic shift (like PB) and >>> for logical shift.
As there is only a difference in the right shift I would not use >>> as logic shift, since one would expect a <<< then for the sake of symmetry.

Instead I would like to have >>> and <<< to be arithmetic rotation operators.

I would then append a different symbol for logic shift and rotation
bye,
Daniel
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Bit shift bug (not a bug)

Post by Olliv »

@DarkDragon

Yep... And rotating is ruled by same bit including (with and without flag). What it causes 4 ways of statements...

Do 174 and 175 ASCII characters belong to a standard?
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Bit shift bug (not a bug)

Post by mk-soft »

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Bit shift bug (not a bug)

Post by Olliv »

@mk-soft

Thanks for the link about your work which is complete and represent all the usual shifts.

I have no idea about an easy syntax which would bypass the blend of digit characters in required functions which become harder to be read.

Code: Select all

change32(n, 5)
not easy to dicern quickly 'change' and '32'...

Also, 'change' expression should not be dicerned between a pseudo-native function and a personnal value or function.

I would suggest

Code: Select all

<^      ^>
<<      >>
^<      >^
<<<       >>>
for the four ways of bit shifting. But it stay a opinion.

Have you got an idea?
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Bit shift bug (not a bug)

Post by mk-soft »

Do you mean toggle bit :?:

Code: Select all


Procedure ToggleBit32(value, bit)
    !xor eax, eax
    !mov ecx, dword [p.v_bit]
    !bts eax, ecx
    !mov ecx, dword [p.v_value]
    !xor eax, ecx
    ProcedureReturn
EndProcedure

Procedure.q ToggleBit64(value.q, bit.q)
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64
    !xor rax, rax
    !mov rcx, qword [p.v_bit]
    !bts rax, rcx
    !mov rcx, qword [p.v_value]
    !xor rax, rcx
    ProcedureReturn
  CompilerElse
    ProcedureReturn (1 << bit) ! value
  CompilerEndIf  
  ProcedureReturn
EndProcedure

a = %11011
c = ToggleBit32(a, 31)
Debug Bin(c, #PB_Long)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Bit shift bug (not a bug)

Post by Olliv »

No, 'change' was just a random expression I took to tell you I do not read easily a function name containing digits in an equation. No link with your useful functions.

Just a wish of new syntax symbols to get directly all the 4 availabilities of bit shift asm ops.

I suggest this :

1) circular bits shift without carry <^ and ^>
2) linear bits shift with sign << and >>
3) circular bits shift with carry ^< and >^
4) linear bits shift without sign <<< and >>>
Post Reply