Page 1 of 1

shr > shl - combing on 1 line

Posted: 16 January 2009, 14:46 PM
by ndudman
Hi

Im trying to run the following code, and combine 4 or so lines into just 1... any help with what Im doing wrong ? res != res2 for some reason ?

Thanks
Neil

Code: Select all

sub Main()
	Dim res as UnsignedInteger
	Dim res2 as UnsignedInteger
	dim t2 as UnsignedInteger = &H7f
	dim t3 as UnsignedInteger = &Hf8
	dim tt as UnsignedInteger
	
	t2 = Shl(t2, 8)
	t3 = t3 AND &HFF
	tt = t2 OR t3
	
	res = Shr(tt , 3)
	
	'~ the above in one line
	res2 = Shr(shl(t2,8) OR (t3 AND &HFF) , 3)
	
	Debug.print "t2="; t2;", t3=";t3; ", tt=";tt; ", res=";res;", res2=";res2;
end sub

Re: shr > shl - combing on 1 line

Posted: 16 January 2009, 16:13 PM
by dkinzer
ndudman wrote:any help with what Im doing wrong ? res != res2 for some reason ?
Well, there are two problems; one your, one ours. The first problem (yours) is that the first sequence modifies t2 so the second (combined) expression has little hope of producing the expected result.

The second problem (ours) is that there is an error in the generated code; there is a missing set of parentheses so the order of operations ends up being incorrect.

You can work around the second problem by storing the intermediate result:

Code: Select all

res2 = shl(t2,8) OR (t3 AND &HFF)
res2 = Shr(res2, 3)
The ultimate AVR code ends up being identical to what it would be if the generated C code were properly parenthesized.

We've corrected the problem internally.

Posted: 17 January 2009, 6:59 AM
by ndudman
Thanks for finding both problems

Neil