Page 1 of 1

Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 1:29 pm
by infratec

Code: Select all

Test.l = $80000000
Debug Hex(Test >> 24, #PB_Long)
It should return 80,
but it returns FFFFFF80

If the highest bit is set, it fills with 1 and not with 0 like it is written in the help.

Code: Select all

Test.l = $7F000000
Debug Hex(Test >> 24, #PB_Long)
Shows 7F, which is correct.

The shift operator should not respect a sign bit.

PB 5.73 x86 on Win10 x64

Re: Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 2:44 pm
by mk-soft
Purebasic bit-shift ">>" is always signed!

Link: Bitshift and Rotation

Code: Select all

; Bitshift right shift
Procedure SHR32(value.l, count.l = 1)

  !mov eax, dword [p.v_value]  
  !mov ecx, dword [p.v_count]
  !shr eax, cl
  ProcedureReturn
  
EndProcedure

lVal.l = $80000000

r1.l = SHR32(lVal, 24)
Debug RSet(Hex(r1), 8, "0")
Exception ASCII and Unicode types (a1.a, u1.u)

Re: Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 4:22 pm
by infratec
Hm ... PB is not assembler

And in the german help is written:
Weggefallene Bits auf der linken Seite der Variablen werden mit 0 aufgefüllt.
No more no less.

You can choose where the bug is, but there is a bug.

Re: Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 4:25 pm
by User_Russian
This error is because PB does not support unsigned variables 4 and 8 bytes.
viewtopic.php?f=3&t=52929

Re: Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 4:35 pm
by mk-soft
Description not complete ...
f.b=-128 >> 1 ; Der Wert von f ergibt -64. -128=%10000000, -64=%11000000. Beim Verschieben nach Rechts bleibt das Vorzeichen ("most significant bit") gleich.

Re: Bug with >> and highest bit set

Posted: Wed Sep 29, 2021 5:45 pm
by infratec
Ok, so the functionality is correct, but the german documentation is wrong.

Sorry, I don't read the examples if the description is clear.

An admin should move this report into the documentation bug section.

Re: Bug with >> and highest bit set

Posted: Sat Mar 11, 2023 9:26 pm
by Andre
Sorry, I don't see, what should be changed in the (german) docs. Any concrete suggestions?

Re: Bug with >> and highest bit set

Posted: Sun Mar 12, 2023 12:15 am
by Demivec
Andre wrote: Sat Mar 11, 2023 9:26 pm Sorry, I don't see, what should be changed in the (german) docs. Any concrete suggestions?
German is not my native language but I would suggest for the description of the '>>' operator in the German text :
Bitweise Verschiebung nach rechts. Durch die Verwendung dieses Operators verschieben Sie jedes Bit der Variablen auf der linken Seite des Operators um die Anzahl der auf der rechten Seite des Operators angegebenen Bits nach rechts. Das Bit ganz links wird repliziert, um alle freien Bitpositionen aufzufüllen, die auf der linken Seite der Variablen weggelassen werden. Wenn das Ergebnis dieses Ausdrucks nicht verwendet wird und sich links vom Operator eine Variable befindet, wird der Wert dieser Variablen verschoben. Es hilft auf jeden Fall, wenn Sie Binärzahlen verstehen, bevor Sie diesen Operator verwenden. Obwohl Sie es auch so verwenden können, als ob Sie jede Position, die Sie verschieben, durch einen Faktor von 2 teilen würden. Hinweis: Die Durchführung dieser Schichtoperation ist in der Regel kostengünstiger als die vergleichbare Teilung.
Likewise, for the English text:
Bitwise right shift. By using this operator, you shift each bit of the variable on the left side of the operator to the right by the number of bits specified on the right side of the operator. The leftmost bit is replicated to fill in any vacant bit positions that are omitted on the left side of the variable. If the result of this expression is not used and there is a variable to the left of the operator, the value of that variable is shifted. It definitely helps if you understand binary numbers before using this operator. Although you can also use it like dividing each position you move by a factor of 2. Note: Performing this layering operation is usually cheaper than the comparable division.

Re: Bug with >> and highest bit set

Posted: Fri Sep 15, 2023 9:42 pm
by Andre
Thanks demivec! Anyway @Fred should take a look, what he exactly wants to be written in the docs 8)