Page 1 of 1

Bit shift bug (not a bug)

Posted: Wed Jun 21, 2017 11:05 am
by Olliv

Code: Select all

X = 1 << 63
Y = X >> 1
Debug Hex(Y)
Should display 40000000 00000000, not
C0000000 00000000.

Re: Bit shift bug

Posted: Wed Jun 21, 2017 11:16 am
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.

Re: Bit shift bug

Posted: Wed Jun 21, 2017 11:55 am
by Olliv
@Darkdragon

Thank you for answering.
Asm allows the coder to bypass this.

Re: Bit shift bug (not a bug)

Posted: Wed Jun 21, 2017 8:25 pm
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

Re: Bit shift bug (not a bug)

Posted: Wed Jun 21, 2017 8:30 pm
by wilbert
I requested it also before.
Both java and javascript have >> operator for arithmetic shift (like PB) and >>> for logical shift.

Re: Bit shift bug (not a bug)

Posted: Sat Jun 24, 2017 6:46 pm
by Olliv
@UserRussian and @Wilbert

Thank you for having recalled already posted infos. I move up UserRussian wish.

Re: Bit shift bug (not a bug)

Posted: Sat Jun 24, 2017 9:10 pm
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

Re: Bit shift bug (not a bug)

Posted: Sat Jun 24, 2017 9:26 pm
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?

Re: Bit shift bug (not a bug)

Posted: Sun Jun 25, 2017 11:56 am
by mk-soft

Re: Bit shift bug (not a bug)

Posted: Sun Jun 25, 2017 1:19 pm
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?

Re: Bit shift bug (not a bug)

Posted: Sun Jun 25, 2017 2:19 pm
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)

Re: Bit shift bug (not a bug)

Posted: Sun Jun 25, 2017 2:40 pm
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 >>>