Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:42:06+00:00 2026-05-11T14:42:06+00:00

I though that I know this one… I had no clue. This simple For

  • 0

I though that I know this one… I had no clue.

This simple For loop:

Dim i As Integer Dim n As Integer = 10 Dim s As Integer = 1  For i = 0 To n Step s     Console.WriteLine(i) Next 

compiles into this (I put it through Refelctor, so it’s easier to read). I couldn’t even get what it does with all these bit-shifts:

Dim n As Integer = 10 Dim VB$t_i4$L1 As Integer = 1 Dim VB$t_i4$L0 As Integer = n Dim i As Integer = 0 Do While (((VB$t_i4$L1 >> &H1F) Xor i) <= ((VB$t_i4$L1 >> &H1F) Xor VB$t_i4$L0))     Console.WriteLine(i)     i = (i + VB$t_i4$L1) Loop 

Why For loop is mutilated like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:42:06+00:00Added an answer on May 11, 2026 at 2:42 pm

    Probably because it’s the ‘generic way’ to cover all cases. Remember that for/step/next can go in any direction with any sign on the increment.

    You used parameters on both the increment and the end-bound. The compiler has no way to know if you are going to count up or down, and if th end-bound is higher or lower than the start bound.

    My guess is this is a way to get code that will work whatever you put in n and s (just a guess, I’m too tired to try and see if that is the case).

    Also it makes copies of the parameters to prevent outside interference (like changing s or n during the enumeration).

    === UPDATE ===

    I doubt anybody is still watching that question but I came back to this nugget just for the sake of completeness, and because I had some time. What VB is doing is a bit sneaky. The bit shift of S basically creates an integer based on the sign of S (it copies the MSB of s, ending up with &hFFFFFFFF is S is negative and &h00000000 if S is positive).

    XOR of an integer value with -1 is equivalent to (-value-1). XOR with 0 is obviously a NOP. So if s is negative, it reverses both values (the -1 cancel each other) to compare them, effectively reversing the order of comparison without the need or a conditional, and thus no jump in the program flow. If s is positive it just compares them.

    so, for s<0 you end up with

    while (-i-1)<=(-n-1)  ==> while -i <= -n ==> while i>=n 

    for s>0 you end up with

    while i <= n 

    Takes me back to my 68k ASM days where such tricks where the everyday stuff (such as XOR.l D0,D0 because XORing a register with itself was faster than loading zero in it…) :p

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
One of my co-workers claims that even though the execution path is cached, there
I know this question has been asked before, but this was one and a
I've found some questions that seem related to this one, but none describes exaclty
I know .net supports base64 encoding of byte arrays. But i thought that i
I know that I can get word completion through CTRL+N & CTRL+P and code
Do you know any tools that can run through the code and find defects
My application uses MS access database to save some sensitive information. Though that information
I am writing a small application with Qt 4.6 (64-bit Arch Linux, though that
I have a problem of web pages being cached even though I specify that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.