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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:35:07+00:00 2026-05-28T15:35:07+00:00

I am unable to understand this why it prints the total binary bits instead

  • 0

I am unable to understand this why it prints the total binary bits instead of last bit in Java and C++.I checked it in C it present as I think i.e only last bit. However, in C++and java it prints all bits.

public static void main(String[] args) {

    toBin(2);
}// end of main

static void toBin(int b) {
    int modu = 0;

    if (b < 1) {
        return;
    }
    modu = b % 2;
    b=(b>>1);
    toBin(b);
    System.out.print(modu);       

}// end of toBin()
  • 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-28T15:35:08+00:00Added an answer on May 28, 2026 at 3:35 pm

    The way toBin() works is that it first finds the remainer when divided by two

    modu = b % 2;
    

    adds that to the beginning, then divides by 2

    b = b >> 1
    

    and repeats recursively until there’s nothing left.

    if (b < 1) {
      return
    }
    

    If you think about it in terms of decimal, it’s a little easier.

    Say you have the number 4863, and you want to print it out in base 10.

    First you take n % 10 which is 3, then you divide by 10, and you 486. Repeat, you have 6 and 48, and so.

    The reason print is after toBin(b) is so it doesn’t need to maintain a string. Instead, it’ll print the most inner recursive call first, and as it exits out, it’ll print the remaining ones in reverse.

    Essentially, the following (which is probably easier to understand) does the same, but prints the digits backwards:

        while (b >= 1) {
            System.out.print(b % 2);
            b /= 2;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying this code on GNU's C++ compiler and am unable to understand
I am unable to understand this error.This error is not in the class that
Here is my code.. but I am unable to understand this code. $('.maxlength') .after(<span></span>)
I'm currently studying indexers chapter, but I'm unable to understand this[int pos] and this[string
I am unable to understand how this works public void addToRule(Rule r) { if
I am unable to understand this loop behavior of the javascript. Can someone tell
I have some questions about basic CSS that I was unable to understand or
I might be missing something here but am unable to understand the true purpose
I know variations of this have been asked before, but I was unable to
my dear Friends I am new to this java ..so plz help me thank

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.