Results 1 to 7 of 7

Thread: [RESOLVED] Syntax Clarification Again

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Resolved [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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Syntax Clarification

    I Ran your first example through a C++ compiler given your values and the result is indeed -252

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  5. #5

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width