7 Replies - 270 Views - Last Post: 06 September 2009 - 09:02 PM Rate Topic: -----

#1 norielsoria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.. :^: :D

#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;
}


*** MOD EDIT: Added code tags. Please :code: ***

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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 norielsoria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

#4 bayson  Icon User is offline

  • New D.I.C Head

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

Re: binary

Posted 06 September 2009 - 06:00 AM

simple muliply the 2nd one with 1000 :) :)
Was This Post Helpful? 0
  • +
  • -

#5 norielsoria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

#6 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 norielsoria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1