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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:29+00:00 2026-05-26T01:20:29+00:00

i am reading section about A Binary Recursive Gcd Algorithm,where this definition is given

  • 0

i am reading section about A Binary Recursive Gcd Algorithm,where this definition is given
let v_2(a) denotes the 2-adic valuation of a, i.e. the number of consecutive zeroes in the least signicant bits of the binary representation of a ,so i am trying to find v_2(a) here is code for this

#include <iostream>
using namespace std;
int main()
{
    int total=0;
    int n,k;
    cout << "enter value n ";
    k=0;
    cin >> n;
    while(k!=1)
    {
        if (k==1)
        {
            break;
        }
        k=n%2;
        n>>=1;
        total++;

    }
    cout<<total<<"  "<<endl;

    return 0;
}

when i enter number 12 (in binary it is 1100) it should give me number 2 but it shows 3,what is wrong?please help me

  • 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-05-26T01:20:30+00:00Added an answer on May 26, 2026 at 1:20 am

    You’re breaking out of the while too late, since you’re running total++ when k==1 has been reached.

    Move the if (k==1) block after the k=n%2; line.

    Note: The k variable isn’t actually necessary. You could simplify the loop to:

    while((n%2)==0) {
      n>>=1;
      total++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading about FB Graph API in this page. Please see the section
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
I've been investigating the out keyword in C# after reading the section about it
Recently i'm reading the book Operating System Concepts Chapter VI about critical section problem,
I have been reading this ebook about DDD and it says that only highly
I'm reading through a C++ book and I'm in a section about reducing the
I am reading a book about MVC2, and in the OutputCache section it states:
OK thought I understood IDipose but just reading the best practices section of Accelerated
I'm reading through K&R and came to the small section on register variables, and
Recently, while reading a Socket Programming HOWTO the following section jumped out at me:

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.