views:

390

answers:

4

Is it possible in C# to divide two binary numbers. All I am trying to do is:

// get int value into binary format, see below

int days = 68;
string binary = Convert.ToString(days, 2);

// but how do you divide the binary numbers? , what format should be used?

01000100 / 000000100 = 4

Little confused any help would be great.

+3  A: 
int a = Convert.ToInt32("01000100", 2);
int b = Convert.ToInt32("000000100", 2);
int c = a / b;

and by the way the answer is dec:17 instead of dec:4

Darin Dimitrov
+5  A: 
// convert from binary representation
int x = Convert.ToInt32("01000100", 2);
int y = Convert.ToInt32("000000100", 2);

// divide
int z = x / y;

// convert back to binary
string z_bin = Convert.ToString(z, 2);
Thomas Levesque
+2  A: 

it is just:

x / y

you don't have to convert integer into binary string by

int days = 68;
string binary = Convert.ToString(days, 2);

numbers are binary in memory.

or i didn't understood you

Andrey
Hi guys Am trying to do is mask binary values like shown below:0100010000000100--------00000100= 4
ChrisMogz
@ChrisMogz - See my answer for and'ing two binary values.
SwDevMan81
+1  A: 

If you are trying to mask the bits together, youll want to use the & Operator

// convert from binary representation
int x = Convert.ToInt32("01000100", 2);
int y = Convert.ToInt32("000000100", 2);

// Bitwise and the values together
int z = x & y; // This will give you 4

// convert back to binary
string z_bin = Convert.ToString(z, 2);
SwDevMan81