# binary

Page 1 of 1

## 7 Replies - 322 Views - Last Post: 06 September 2009 - 09:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124209&amp;s=f8db9d9672b15be7454ba965620488bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 norielsoria

Reputation: 0
• Posts: 16
• Joined: 14-August 09

# binary

Posted 06 September 2009 - 01:38 AM

hey there...i would like to ask you guys on how to put this into code.. my prob is
ex.
my first input is 1110 then my second input is 1101101.. i want add 3 zero to the 2nd input like 1101101000..
i am doing division. ans= second/first

help guys..

```#include <iostream.h>
#include <conio.h>
#include <math.h>
void binary(int);
void binary1(int);

int first, second, divisor, number;
void main()
{

cout << "Enter an integer: ";
cin >> first;
cout << "Enter another integer: ";
cin >> second;

divisor = first / second;

number = first % second;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
binary1(first);
binary(number);
cout << endl;
}
}

void binary(int number) {
int remainder;

if(number <= 1) {
cout << number;
return;
}

remainder = number%2;
binary(number >> 1);
cout << remainder;
}

void binary1(int first) {
int remainder1;

if(first <= 1) {
cout << first;
return;
}

remainder1 = first%2;
binary(first >> 1);
cout << remainder1;
}
```

This post has been edited by JackOfAllTrades: 06 September 2009 - 05:27 AM

Is This A Good Question/Topic? 0

## Replies To: binary

### #2 debjit625

• D.I.C Regular

Reputation: 51
• Posts: 446
• Joined: 06-September 08

## Re: binary

Posted 06 September 2009 - 01:41 AM

Sorry you have to show some code that you have worked on after that we can help you on the problems you get

Good Luck

### #3 norielsoria

Reputation: 0
• Posts: 16
• Joined: 14-August 09

## Re: binary

Posted 06 September 2009 - 04:33 AM

am i to array on these??? i will only merge my second with zeros.. corresponding to my (first input -1) situation??

### #4 bayson

Reputation: 0
• Posts: 1
• Joined: 05-September 09

## Re: binary

Posted 06 September 2009 - 06:00 AM

simple muliply the 2nd one with 1000

### #5 norielsoria

Reputation: 0
• Posts: 16
• Joined: 14-August 09

## Re: binary

Posted 06 September 2009 - 07:03 AM

1101101 and " 1101101+0000" is not the same... i cant multiply it by 1000. how can i do this??

### #6 debjit625

• D.I.C Regular

Reputation: 51
• Posts: 446
• Joined: 06-September 08

## Re: binary

Posted 06 September 2009 - 07:35 AM

Hey are you trying to do binary calculation ? if yes then you are going in a wrong way and if not then why cant you multiply the 2nd integer with 1000.
```second *= 1000;

```

This post has been edited by debjit625: 06 September 2009 - 07:38 AM

### #7 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: binary

Posted 06 September 2009 - 08:29 AM

ok... I read over the thread and I have NO idea what is going on... but lets talk a little about binary (maybe we will hit upon what you are looking for).

You said you want to add 3 zeros to the right of the binary number.

1101101 becomes 1101101000

1101101 = 109,

109 * 2 = 11011010 = 218
218 * 2 = 110110100 = 109 * 4 = 436
436 * 2 = 1101101000 = 109 * 8 = 109 * 8 = 872

This pattern holds for any positive binary number, multiplication by 2 shits the bits to the left, and adds a zero to the right. So to multiply a number by 2 to the X power I just need to add X zeros to the right (shoft X times to the left).

SO, if you want to add 3 zeros to the right of an integer in C/C++ you have two options:
option #1 multiply by 8 (2 to the 3rd power).
option #2 use the binary shift operator <<

Note if you want to move the bits of a number to the left you DIVIDE by 2, or use the right shift (>>) -- this is what you are doing in your binary() recursive function call.

### #8 norielsoria

Reputation: 0
• Posts: 16
• Joined: 14-August 09

## Re: binary

Posted 06 September 2009 - 09:02 PM

tnx nick... that is somewhat the answer.. hehehe but its better if u can edit my code for that..hehe..