Jump to content

bit representation 2

- - - - -

  • Please log in to reply
4 replies to this topic

#1
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
Hi
Finally I could write this class to print binary representation of a basic type:

class Bits

{

    static void print(byte[] b, bool newline = true)

    {

        for(int i = b.Length - 1; 0 <= i; i--)

        {

            for(int j = 8; 1 <= j; j--)

                Console.Write(((byte)(b[i] << (8 - j))) >> 7);

            Console.Write(" ");

        }       

    }

    public static void Show<T>(T x)

    {

        if(typeof(T) == typeof(float))        

            print(BitConverter.GetBytes(float.Parse(x.ToString())));

        else if(typeof(T) == typeof(double))

            print(BitConverter.GetBytes(double.Parse(x.ToString())));

        else if(typeof(T) == typeof(decimal))

        {

            int[] Bits = decimal.GetBits(decimal.Parse(x.ToString()));

            for(int i = 0; i < Bits.Length; i++)                

                print(BitConverter.GetBytes(Bits[i]));

        }

        else if(typeof(T) == typeof(sbyte))

            print(BitConverter.GetBytes(sbyte.Parse(x.ToString())));

        else if(typeof(T) == typeof(short))

            print(BitConverter.GetBytes(short.Parse(x.ToString())));

        else if(typeof(T) == typeof(int))

            print(BitConverter.GetBytes(int.Parse(x.ToString())));

        else if(typeof(T) == typeof(long))

            print(BitConverter.GetBytes(long.Parse(x.ToString())));

        else if(typeof(T) == typeof(byte))

            print(BitConverter.GetBytes(byte.Parse(x.ToString())));

        else if(typeof(T) == typeof(ushort))

            print(BitConverter.GetBytes(ushort.Parse(x.ToString())));

        else if(typeof(T) == typeof(uint))

            print(BitConverter.GetBytes(uint.Parse(x.ToString())));

        else if(typeof(T) == typeof(ulong))

            print(BitConverter.GetBytes(ulong.Parse(x.ToString())));

        else if(typeof(T) == typeof(char))

            print(BitConverter.GetBytes(char.Parse(x.ToString())));

        else if(typeof(T) == typeof(bool))

            print(BitConverter.GetBytes(bool.Parse(x.ToString())));

        Console.Write("\n\n");

    }

}


How to use the class:

decimal a = 123.123m;

Bits.Show(a);

Output:

Quote

00000000 00000001 11100000 11110011 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000011 00000000 00000000

there are a few problems:
1) sbyte.Parse(x.ToString()) is not so interesting. converting to string and then to the original number.
2) I'm not sure about decimal type.
3) show method is a template (generic), but it's definition is not like a template function. in C++ I don't need a lot of if else statements.
I would appreciate it, if you can help me solve these problems.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What you're trying to do isn't easy in C#. C/C++ are unmanaged, meaning they let the programmer have direct and unrestricted access to memory. This is also dangerous, as unpredictable data corruption may occur if the programmer makes mistakes with direct access to memory like this. On the other hand, C# is managed, which means the programmer is freed from responsibility of working directly in the memory. C# objects are designed to forbid the programmer from having any kind of direct access to the memory, instead requiring all operations to be done through the C# language and .NET framework, thus, making things safer.

If you're looking for a all-inclusive way of looking at the in-memory bit representations of objects, I suggest you look into what lespauled suggested in your previous thread and go with Marshalling.

Here's a dialog on marshalling:
What is Marshaling? - C# / C Sharp

Documentation:
Marshal Class (System.Runtime.InteropServices)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
Thanks. it shortened the class

class Bits

{

    static void print(byte[] b)

    {

        for(int i = b.Length - 1; 0 <= i; i--)

        {

            for(int j = 8; 1 <= j; j--)

                Console.Write(((byte)(b[i] << (8 - j))) >> 7);

            Console.Write(" ");

        }       

    }

    public static void show<T>(T x)

    {

        int size = Marshal.SizeOf(x);

        byte[] b = new byte[size];

        IntPtr p = Marshal.AllocHGlobal(size);

        Marshal.StructureToPtr(x, p, false);

        Marshal.Copy(p, b, 0, Marshal.SizeOf(x));

        print(b);

        Marshal.FreeHGlobal(p);

    }

}


but bit representation of decimal is somewhat different from previous class. I think something is going wrong.
And I am uncertain about AllocHGlobal. I prefer to use something like new in C++;
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
'new' is going to create objects within the managed environment of C#, which is going backwards again from where you just were. If you want to change the behavior of the system, you have to play by different rules. :)

As for the bit representation of decimal, I'm not knowledgeable myself on how C# stores decimal format in memory. Unless someone else here can tell you, my best suggestion would be to go do some searching for any documentation that explains the decimal memory storage format and compare that with the results you're getting from your code above to determine whether you are getting correct answers.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
on this page:
Decimal.GetBits Method (System)
bit combination of decimal is determined.

I think my first class must be changed:

class Bits

{

    static void print(byte[] b)

    {

        for(int i = b.Length - 1; 0 <= i; i--)

        {

            for(int j = 8; 1 <= j; j--)

                Console.Write(((byte)(b[i] << (8 - j))) >> 7);

            Console.Write(" ");

        }       

    }

    

    public static void Show<T>(T x)

    {

        if(typeof(T) == typeof(float))

            print(BitConverter.GetBytes(float.Parse(x.ToString())));

        else if(typeof(T) == typeof(double))

            print(BitConverter.GetBytes(double.Parse(x.ToString())));

        else if(typeof(T) == typeof(decimal))

        {

            int[] Bits = decimal.GetBits(decimal.Parse(x.ToString()));

            print(BitConverter.GetBytes(Bits[1]));

            print(BitConverter.GetBytes(Bits[0]));

            print(BitConverter.GetBytes(Bits[2]));

            print(BitConverter.GetBytes(Bits[3]));

        }

        else if(typeof(T) == typeof(sbyte))

            print(BitConverter.GetBytes(sbyte.Parse(x.ToString())));

        else if(typeof(T) == typeof(short))

            print(BitConverter.GetBytes(short.Parse(x.ToString())));

        else if(typeof(T) == typeof(int))

            print(BitConverter.GetBytes(int.Parse(x.ToString())));

        else if(typeof(T) == typeof(long))

            print(BitConverter.GetBytes(long.Parse(x.ToString())));

        else if(typeof(T) == typeof(byte))

            print(BitConverter.GetBytes(byte.Parse(x.ToString())));

        else if(typeof(T) == typeof(ushort))

            print(BitConverter.GetBytes(ushort.Parse(x.ToString())));

        else if(typeof(T) == typeof(uint))

            print(BitConverter.GetBytes(uint.Parse(x.ToString())));

        else if(typeof(T) == typeof(ulong))

            print(BitConverter.GetBytes(ulong.Parse(x.ToString())));

        else if(typeof(T) == typeof(char))

            print(BitConverter.GetBytes(char.Parse(x.ToString())));

        else if(typeof(T) == typeof(bool))

            print(BitConverter.GetBytes(bool.Parse(x.ToString())));

        Console.Write("\n\n");

    }   

}


I wrote this ebook! Will you translate it into English for free!?:confused: PM me!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users