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 103081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:04:02+00:00 2026-05-11T01:04:02+00:00

I have this If condition in VB6 If ( X AND ( 2 ^

  • 0

I have this If condition in VB6

If ( X AND ( 2 ^ Y)) Then    a = a + ' 1'  Else   a = a + ' 0'  

I want the same equivalent in C#

I tried doing like

if ( X && ( 2 ^ Y))   // ERROR: && can not be used between int to int   a = a + '1'; else   a = a + '0'; 

but this thing is giving me an error.

Here is my complete VB code that I want to convert into C#

For i = 7 To 0 Step -1         If intNumber And (2 ^ i) Then   ' Use the logical 'AND' operator.             bin = bin + '1'         Else             bin = bin + '0'         End If     Next 

In above code intNumber could be any number.

  • 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-11T01:04:02+00:00Added an answer on May 11, 2026 at 1:04 am

    Note: This answer has been heavily edited due to lack of information in original question. This version of the answer is now based on the updated question with enough information. For history, check the edit log.

    Two things:

    • && is used between Boolean expressions, to determine the logical AND-value
    • ^ in C# means XOR, not raised-to-the-power-of. You didn’t ask a question about this but it was inevitable that you discovered that ^ didn’t appear to be doing its job.

    The && is easily handled, since it can be replaced with a single & instead, which has a dual meaning, depending on context. Either it is a full-evaluation logical AND operator (&& is a short-circuited one), or it is a bitwise operator, which is what you want here.

    The ^ is different though. The most direct equivalent is Math.Pow, but in this case, a better alternative is available, bit-shift.

    The case of 2^X can be thought of as shift the 1-bit X positions to the left, and shift bits to the left has its own operator, the << operator.

    So 2^X can be replaced with 1 << X.

    In this context, here’s what you want for your inner-most if-statement:

    if ((intNumber & (1 << index)) != 0)     a = a + '1'; else     a = a + '0'; 

    Plug that into a loop like you have in your bottom example, and you get this:

    for (Int32 index = 7; index >= 0; index--)     if ((intNumber & (1 << index)) != 0)         bin = bin + '1';     else         bin = bin + '0'; 

    Now, concatenating strings like this generates GC pressure, so you should probably either store these digits into a Char array, and construct the string afterwards, or use the StringBuilder class. Otherwise you’re going to build up 8 (from my example) differently sized strings, and you’re only going to use and keep the last one. Depending on circumstances this might not pose a problem, but if you call this code many times, it will add up.

    Here’s a better version of the final code:

    Char[] digits = new Char[8]; // change if you want more/fewer digits for (Int32 index = 0; index < digits.Length; index++)     if ((intNumber & (1 << index)) != 0)         digits[digits.Length - 1 - index] = '1';     else         digits[digits.Length - 1 - index] = '0'; bin = new String(digits); 

    However, and here’s the kicker. There is already a way to convert an Int32 value to a string full of binary digits in .NET, and it’s the Convert.ToString method. The only difference is that it doesn’t add any leading zeros, so we have to do that ourselves.

    So, here’s the final code you should be using:

    String bin = Convert.ToString(intNumber, 2).PadLeft(8, '0'); 

    This replaces the whole loop.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your sql server supports it you can create an… May 12, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer The same was you would do it in any C/C++/Objective-C… May 12, 2026 at 12:18 am
  • Editorial Team
    Editorial Team added an answer This probably won't work how you expect it to. Instead,… May 12, 2026 at 12:18 am

Related Questions

I am working on a legacy app in VB6 and am wondering what the
I have an application written in C#.Net (Framework 2.0 if it matters). It calls
I have a stored procedure on a SQL Server 2005 database which has a
I don't know OWNER object class name. So I must check everywhere in my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.