|
-
Dec 15th, 2011, 09:18 AM
#1
[RESOLVED] Syntax Clarification Again
Good day. I'm not a C programmer but am trying to convert some code from C to VB and would appreciate clarification on a few C-related lines of code.
Example 1) value += ((-1) << x ) + 1;
value & x are both type int. I don't know how C shifts negative values but if someone can give me the result to the equation, I can figure out the rest on my own. Assume value is 3 and x is 8.
Example 2) if (!((x1 = blk[8*4] << 8) ...);
blk is an array. This line of code is first assigning x1 to blk[8*4] shifted left or is x1 assigned to blk[8*4] without the shift applied?
Example 3) x1 = x2 = x3 = x1 << 3
End result will be x1,x2,x3 will all have same value which is x1 shifted left, correct?
There are several hundred lines of code I'm trying to convert, and have encountered only a few situations where I'm doubting my interpretation. Thanks in advance.
Last edited by LaVolpe; Dec 20th, 2011 at 10:29 AM.
-
Dec 15th, 2011, 08:09 PM
#2
Re: Syntax Clarification
Example 1 is nearly identical in VB.
Example 2 is:
Code:
x1 = blk(8 *4) << 8
If Not x1 Then
Example 3 is:
Code:
x3 = x1 << 3
x2 = x3
x1 = x2
-
Dec 16th, 2011, 09:36 AM
#3
Re: Syntax Clarification
David, thanx for your reply. For example 1, you stated "nearly identical".
So given the following, in VB the result would be -252. Same for C?
Code:
value = 3
x = 8
' value += ((-1) << x ) + 1;
value = value + (-1 * 256) + 1
-
Dec 16th, 2011, 10:47 AM
#4
Re: Syntax Clarification
I Ran your first example through a C++ compiler given your values and the result is indeed -252
-
Dec 16th, 2011, 11:06 AM
#5
Re: Syntax Clarification
Thank you both. If I encounter another line of code that I'm iffy on, I'll be sure to post back
-
Dec 16th, 2011, 11:19 PM
#6
Re: [RESOLVED] Syntax Clarification
For 'example 1', you don't have to change it as much as you did - you just have to remove the semi-colon (that's why I said it was nearly identical):
Code:
value += ((-1) << x) + 1
This works in VB and the result is -252.
-
Dec 17th, 2011, 06:51 AM
#7
Re: [RESOLVED] Syntax Clarification
I'm pretty sure LaVolpe is referring to VB classic which does not have any shift or compound assignment operators.
Because VB classics raise operator '^' is so horribly slow it makes a lot of sense to employ a small LUT to store the powers of two (BitLUT(0) = 1 ... BitLUT(10) = 1024 etc)
Code:
value = value + (-1 * BitLUT(x)) + 1
simplifies to
Code:
value = value - BitLUT(x) + 1
overflow checking turned off of course.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|