shr > shl - combing on 1 line

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

shr > shl - combing on 1 line

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: shr > shl - combing on 1 line

Post 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.
- Don Kinzer
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Post by ndudman »

Thanks for finding both problems

Neil
Post Reply