4

I'm trying to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11 right?

Here's my code:

static void Main(string[] args)
{
 Console.WriteLine("Enter a Number");
 int numb = int.Parse(Console.ReadLine());
 int reverse = 0;
 while (numb > 0)
 {
 int rem = numb % 10;
 reverse = (reverse * 10) + rem;
 numb = numb / 10;
 }
 Console.WriteLine("Reverse number={0}", reverse);
 Console.ReadLine();
}

By this code I only get the numbers to reverse (13 -> 31)...

The input should contain a single line with an integer N, 1≤N≤1000000000 and I want my output in one line with one integer, the number I want to get by reversing the binary representation of N.

Alexei Levenkov
101k15 gold badges137 silver badges189 bronze badges
asked Sep 24, 2015 at 15:11
1
  • 3
    "By this code I only get the numbers to reverse..." - Yes, because you're using 10 everywhere, which means you're getting the decimal digits, not the binary ones... Commented Sep 24, 2015 at 15:15

3 Answers 3

8

Something like that

// 13 = 1101b
int value = 13;
// 11 = 1011b
int result = Convert.ToInt32(new String(
 Convert.ToString(value, 2)
 .Reverse()
 .ToArray()), 2);

Explanation:

  • Convert.ToString(value, 2) returns value in binary representation ("1101")
  • Reverse().ToArray() - reverse the string ('1','0','1','1') as sequence of characters and converts to array char[].
  • new String(...) constructs string "1011" from array of char
  • finally, Convert.ToInt32(..., 2) convert binary representation back to int
Alexei Levenkov
101k15 gold badges137 silver badges189 bronze badges
answered Sep 24, 2015 at 15:16

Comments

0

You can use Convert.ToString and Convert.ToInt32 methods, where 2 means binary:

int numb = int.Parse(Console.ReadLine());
var reversedString = Convert.ToString(numb, 2).ReverseString();
var result = Convert.ToInt32(reversedString, 2);
...
public static string ReverseString(this string s)
{
 char[] arr = s.ToCharArray();
 Array.Reverse(arr);
 return new string(arr);
}
answered Sep 24, 2015 at 15:16

Comments

0

A fun excercise would be doing this without using the string conversion.

I have very little experience with bit twiddling so there is probably a faster and better way of doing this, but this seems to work:

 public static IEnumerable<bool> ToBinary(this int n)
 {
 for (int i = 0; i < 32; i++)
 {
 yield return (n & (1 << i)) != 0;
 }
 }
 public static int ToInt(this IEnumerable<bool> b)
 {
 var n = 0;
 var counter = 0;
 foreach (var i in b.Trim().Take(32))
 {
 n = n | (i ? 1 : 0) << counter;
 counter++
 }
 return n;
 }
 private static IEnumerable<bool> Trim(this IEnumerable<bool> list)
 {
 bool trim = true;
 foreach (var i in list)
 {
 if (i)
 {
 trim = false;
 }
 if (!trim)
 {
 yield return i;
 }
 }
 }

And now you'd call it like this:

var reversed = n.ToBinary().Reverse().ToInt();
answered Sep 24, 2015 at 16:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.