LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2011, 08:08 AM   #1
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Rep: Reputation: Disabled
binary to decimal


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" ?

thanks for explanation
 
Old 12-31-2011, 08:20 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by newbie0101 View Post
but what means this: "<< 1" ?
a << 1 means: Take the value of a and shift-left by 1 bit position. Likewise, there's the >> operator, which is a right-shift.

[X] Doc CPU
 
Old 12-31-2011, 08:22 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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?
 
Old 12-31-2011, 08:26 AM   #4
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Quote:
Originally Posted by newbie0101 View Post
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" ?
In this context, "<<" is the left shift operator. It's shifting the bits that make up 'number' one to the left.
 
Old 12-31-2011, 08:54 AM   #5
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Original Poster
Rep: Reputation: Disabled
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)
 
Old 12-31-2011, 11:02 AM   #6
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Quote:
Originally Posted by newbie0101 View Post
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.

Hope this helps,
Lyle.
 
Old 01-01-2012, 08:17 AM   #7
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by lyle_s View Post
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 View Post
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 View Post
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.

[X] Doc CPU
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal to Binary to Hex howto Gortex Linux - Newbie 7 03-05-2009 03:29 PM
Decimal to binary conversion master General 10 12-21-2007 02:03 AM
how to convert binary fractions to decimal jabka Programming 12 12-16-2006 06:05 PM
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration