leading 0's padding. Is it possible in c++?

Status
Not open for further replies.

desijays

Broken In
i need to store the number '00011' in an integer variable but since C++ considers the 3 zeros insignificant, it drops them by default and only the number 11 gets stored in the variable.. How can i store the binary number as it is...... can someone help.

im trying to write a function that manipulates each bit in the binary number. ... example if the number i entered is 11001, the function returns the first and second bit correctly but then it ignores the 0's and directly proceeds to the next signicficant bit which is '1' in this case. i hope i'm not confusing..

i want to be able to make the function return the 0 as well, if it is present in the binary number...
 

Sykora

I see right through you.
You must store the number as a string, and operate on the ascii values instead of treating it as an integer. You can look up any table to find the ascii values of the digits 0-9. Then extract each one, and operate on them as you will. Using an integer data type is hard because it will get confused between the binary you are asking it to represent and the decimal it's been programmed to represent.
 
OP
D

desijays

Broken In
That is one option mate. I already know that.. But i was wondering if I can do that without using the strings. I heard there is a stream manipulator that facilitates the inclusion of 0's anywhere in the number...

for you're information i'm posting the program as well... sorry if its long.

here is the code i'm working on, just in case

the execution goes like this.

the user enters a binary number and the program outputs the decimal equivalent. The problem is in the "getRemainder(int)" function. As you know, binary numbers consist of only 0 and 1. The "getRemainder(int) function is used to pick off each bit in the number. as long as it encounters only 1 it works fine. but the moment it encounters a 0 it ignores it completely, since it has no value. if you can't understand me, just debug the getRemainder(int) function using visual C++. won't take long. its just 5 lines long. as input, enter only binary numbers. go ahead and use decimal numbers, but i don't know how it will react.

I'm sorry abt using my own header file for the cout and cin statements. since you won't be able to execute it, just start a new project, copy the function and pass a 5 digit binary number to it like say 11001, 10011 or anything like that.

Code:
#include "print.h"

int divisor = 10000;

int getQuotient( int );
int getRemainder( int );
int getMultiplicand( int );

int main()
{
	int BinaryNumber;
	int InduvidualBit;
	int Multiplicand;
	int DecimalEquivalent = 0;
	int i = 1;

	Print( "Enter five bit binary number : " );
	BinaryNumber = InputInt();
	
	while( i <= 5 )
	{
		InduvidualBit = getQuotient( BinaryNumber );
		Multiplicand = getMultiplicand( i );
		DecimalEquivalent = DecimalEquivalent + ( InduvidualBit * Multiplicand );
		BinaryNumber = getRemainder( BinaryNumber );
		i++;
	}
	
	Print( "\n" );
	Print( DecimalEquivalent );
	return 0;
}

int getQuotient( int number )
{
	int quotient;

	quotient = number / divisor;
	
	return quotient;
}

int getRemainder( int number )
{
	int remainder;

	remainder = number % divisor;
	divisor = divisor / 10;

	return remainder;
}

int getMultiplicand( int number )
{
	int i = 1;
	if( number == 1 )
		return i;
	else
	{
		int num = 1;
		while( i < number )
		{
			num = num * 2;
			i++;
		}
		return num;
	}
}
 
Status
Not open for further replies.
Top Bottom