Is it possible in C# to divide two binary numbers. All I am trying to do is:
Get integer 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.
// 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);
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With