Inconsistency in Bit shift right

Report problems with documented functionality
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Inconsistency in Bit shift right

10 May 2016, 19:24

Bit shift right takes the sign flag and moves it to the next Position however it does not change the content of the flag itself.
This behavior is inconsistent to it's counterpart bit shift left which never has to regard the flag in any operation and also any other language implementing this feature.

Code: Select all

Msgbox % ( ((1<<63)>>1) = (1<<62) )
The workaround for now would be:

Code: Select all

bitshiftright(value,bits)
{
	if bits
		value := ( value >> bits ) & 0xE777777777777777 
	return value
}
Recommends AHK Studio
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Inconsistency in Bit shift right

14 May 2016, 19:27

The current behaviour is correct. The >> operator is implemented in terms of the MSVC++ (int64) >> operator, and therefore has identical behaviour.

See Arithmetic shift vs Logical shift.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Inconsistency in Bit shift right

08 Jan 2017, 12:03

Well the merits of having this strictly typed operator in a loosely typed language is questionable.
I'm just here to fix the function I mentioned in my post:

Code: Select all

SetFormat,Integer,H

bitshiftright(value,bits)
{
	if ( bits ) 
		value := ( value >> 1 ) & ~( 1 << 63 )
	return value >> ( bits - !( !bits )  )
}

bitshiftright2(value,bits)
{
	if bits
		value := ( value >> bits ) & 0xE777777777777777 
	return value
}

Msgbox % bitShiftright( -1, 63 ) . "`n" . bitshiftright2( -1, 63 ) . "`n" . -1 >> 63
Recommends AHK Studio

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 46 guests