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

  • Home
  • SEARCH
  • 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 8386839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:03:11+00:00 2026-06-09T18:03:11+00:00

The following Pseudo and JavaScript code is a extract from the implementation of a

  • 0

The following Pseudo and JavaScript code is a extract from the implementation of a algorithm , i want to convert it to C++ .

Pseudo Code :

for b from 0 to 2|R| do
for i from 0 to |R| do
if BIT-AT(b, i) = 1 then // b’s bit at index i

JavaScript Code :

for (var b = 0; b < Math.pow(2, orders[r].length); b++) // use b's bits for directions
   {
   for (var i = 0; i < orders[r].length; i++)
    {
    if (((b >> i) & 1) == 1) {  // is b's bit at index i on? 

I don’t understand what is happening in the last line of this code , What Should be the C++ code for the above given JavaScript code . So far what i have written is :

for (int b = 0; b < pow(2, orders.at(r).size()); b++) 
{
  for (int i = 0; i < orders.at(r).size(); i++)
    {
     if (((b >> i) & 1) == 1)***//This line is not doing what it is supposed to do according to pseudo code*** 

The last line is giving me segmentation fault .

—
Edit:I apologize the problem was somewhere else , This code works fine .

  • 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. Editorial Team
    Editorial Team
    2026-06-09T18:03:13+00:00Added an answer on June 9, 2026 at 6:03 pm
    (((b >> i) & 1) == 1)
         |     |
         |     |
         |    bitwise AND between the result of the shift and number 1.
         |
        shift b by i bits to the right
    

    After that the result is compared with the number 1.

    So if, for example, b is 8, and i is 2, it will do the following:

    1. shift 8 (which is 00001000) by 2 bits to the right. The result will be 00000100.
    2. apply the bitwise AND: 00000100 BITWISE_AND 00000001, the result will be 0.
    3. Compare it with 1. Since 0 =/= 1, you will not enter that last if.

    As for the logic behind this, the code ((b >> i) & 1) == 1) returns true if the bit number i of the b variable is 1, and false otherwise.

    And I believe that c++ code will be the same, with the exception that we don’t have Math class in c++, and you’ll have to replace vars with the corresponding types.

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

Sidebar

Related Questions

I want to build the following pseudo query Select a From APDU a where
I would like do do the following in Javascript (pseudo code): myString.replace(/mypattern/g, f(currentMatch)); that
The following pseudo-code is from the first chapter of an online preview version of
I want your suggestion on the following pseudo-code. Please suggest how could I improve
I want to accomplish the equivalent of the following pseudo-code in bash (both a
The following (pseudo-) code induces some behaviour which I dont understand. I have 2
I am currently working with the following pseudo code block to fix a regex
I think I might need to do something like the following pseudo-code in my
I can drop a SqlServer Backup Device using SQL-DMO using the following pseudo-code: SQLDMO.SQLServer2
The following is pseudo code of an integration test that is failing: [Test] void

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.