Home > OS >  How to encode a decimal number to binary in 16 bits in C#?
How to encode a decimal number to binary in 16 bits in C#?

Time:06-17

The problem is asking :

The user gives me integer n,

I convert it to binary in 16 bits,

inverse the binary,

then decode the inverse binary into a new integer.

example:

14769 is 0011100110110001 (the 2 zeros in the front are the problem for me)

inverse the binary:

1000110110011100

Decode:

36252

I wrote the code but when I convert to binary it only gives me 11100110110001 without 00 in front, so the whole inverse binary will change and the new integer will be different.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace HelloWorld
{
  public class Program
  {
  public static void Main(string[] args)
  {
    long n, n1, p, i, r, sum, inv, inv1, newint;
    Console.WriteLine("give n:");
    n=long.Parse(Console.ReadLine());
    n1=n;
    p=1;
    sum=0;
    i=n;
    //for below is for the binary representation of n
    for(i=n;i!=0;i=i/2)
    {
      r=i%2;
      sum=sum r*p;
      p=p*10;        
   }

   inv=0;
   //for below is to inverse the above binary representation 
   for(i=sum;i!=0;i=i/10) 
   {
     r=i;
     inv=10*inv r;       
   }

   inv1=inv;
   newint=0;
   p=0;
   //for below is to decode the inverse binary to its decimal representation 
   for(i=inv;i!=0;i=i/10)
   {
     r=i;
     newint=newint r*(long)Math.Pow(2,p);
     p=p 1;
   }
   Console.WriteLine("The number that you gave = {0} \nIts binary 
   representation = {1} \n\nThe inverse binary representation = {2} \nThe integer corresponding to the inverse binary number = {3}", n1, sum, inv1, newint); 
   } 
 }
 }

So how can i encode on 16 bits?

Edit:

1)We didn't learn built in functions

2)We didn't learn padding or Convert.Int...

3)We only know the for loop ( while loop but better not use it)

4)We can't use strings either

CodePudding user response:

You can try using Convert to obtain binary representation and Aggregate (Linq) to get back decimal:

using System.Linq;
...

int value = 14769;
      
int result = Convert
  .ToString(value, 2) // Binary representation
  .PadLeft(16, '0')   // Ensure it is 16 characters long
  .Reverse()          // Reverse
  .Aggregate(0, (s, a) => s * 2   a - '0'); // Back to decimal

Console.Write($"{value} => {result}");

Output:

14769 => 36252

Edit: Loop solution (if you are not allowed to use the classes above...)

int value = 14769;
int result = 0;

for (int i = 0, v = value; i < 16;   i, v /= 2)
  result = result * 2   v % 2;

Console.Write($"{value} => {result}");

CodePudding user response:

You could reverse the bits using some simple bitwise operators.

ushort num = 14769;
ushort result = 0;

// ushort is 16 bits, therefore exactly 16 iterations is required    
for (var i = 0; i < 16; i  , num >>= 1){
    // shift result bits left by 1 position
    result <<= 1;

    // add the i'th bit in the first position
    result |= (ushort)(num & 1);
}

Console.WriteLine(result); //36252

  •  Tags:  
  • c#
  • Related