ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello guys, i am a beginner of C++ and to my mind came an idea to write a program that converts decimal to binary.
before i decided to write my own i was looking for some examples in google and found this:
Code:
#include <iostream>
using namespace std;
void binary(int);
int main() {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
binary(number);
cout << endl;
}
}
void binary(int number) {
int remainder;
if(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number << 1);
cout << remainder;
}
it works but i don't understand this line:
Code:
binary(number << 1)
i know that the binary function takes one argument to work with but what means this: "<< 1" ?
It looks like a recursive call to the binary() method, where the arguement is a bit-wise shifted value. A single shift will usually double or half a number in binary. Does this help you understand the algorithm?
thank you very much for such quick response but, why does it shift to the right by 1 bit and takes of the value of number actually ? (by the way there is a mistake in the code, there is number << 1 and correct is: number >> 1, was just experimentig)
why does it shift to the right by 1 bit and takes of the value of number actually ?
That line combines at least three concepts: recursion (the function called 'binary' calling itself), overloaded operators (>>), and temporary values (the fact that the inner call to 'binary' is called with an argument not stored in a named variable.)
I'll try to separate them out for you.
The line 'binary(number >> 1);' can be replaced with:
Code:
temp = number >> 1;
binary(temp);
If the line 'temp = number << 1;' still causes trouble, it works the same way as 'temp = number + 1;' in that plus operator takes two arguments and returns a result, just like right shift.
You've probably seen '<<' and '>>' used in input/output. The '<<' and '>>' operators are what is called "overloaded." This means that the functions that get called are dependant on the types the operator is called with.
So, in
Code:
cout << "happy times\n";
and
Code:
int shifted = 10 << 1;
completely different functions are being called, even though the same operator, <<, is used. You can write functions to overload operators to work with your own classes.
That line combines at least three concepts: recursion (the function called 'binary' calling itself), overloaded operators (>>), and temporary values (the fact that the inner call to 'binary' is called with an argument not stored in a named variable.)
there is no overloading here, as the bit shift is the native operation of >> or <<.
Quote:
Originally Posted by lyle_s
You've probably seen '<<' and '>>' used in input/output. The '<<' and '>>' operators are what is called "overloaded."
Yes, they're overloaded for use with C++ iostreams as their first operand.
Quote:
Originally Posted by lyle_s
Code:
cout << "happy times\n";
and
Code:
int shifted = 10 << 1;
completely different functions are being called, even though the same operator, <<, is used.
Right, like the * operator has a different meaning depending on its right operand. If that is a pointer expression, the * is the dereferencing operator; if it's a number (or can be casted to one), it is the multiplication operator.
And that's not even C++, it's plain C.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.