Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hi I need some help with this code

Hi I need some help with this code

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Angelinna
    wrote on last edited by
    #1

    /method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); //string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; It is givingme a wrong conversion from decimal to two's complement and I can figure out the reason. Any help please?

    C 1 Reply Last reply
    0
    • A Angelinna

      /method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); //string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; It is givingme a wrong conversion from decimal to two's complement and I can figure out the reason. Any help please?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What are you trying to do ? Where is bitstring defined ? Obviously, it's not reset in this function, so whatever value it had, 2 is added to every element. I don't see how you remotely are interacting with the decimal, except as an index into an array. So, if your array has 8 elements, as it seems, a number > 8 will just crash it, right ?

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      A 1 Reply Last reply
      0
      • C Christian Graus

        What are you trying to do ? Where is bitstring defined ? Obviously, it's not reset in this function, so whatever value it had, 2 is added to every element. I don't see how you remotely are interacting with the decimal, except as an index into an array. So, if your array has 8 elements, as it seems, a number > 8 will just crash it, right ?

        Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

        A Offline
        A Offline
        Angelinna
        wrote on last edited by
        #3

        This is where I started from, thanks. ............................................................. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TwosComplement { public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int i = 0; i < 8; i++) { bitstring[i] = 0;} twoc_output.Text = "enter an integer"; decimal_output.Text = "enter a length 8 bitstring"; } private int[] bitstring = new int[8]; private int nDecimal; //strings for reading the bitstring in and out private string instring, outstring; private string nextbit; //helper function for flipping bits in the bitstring private int negate(int x) { if (x == 1) return 0; return 1; } //method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; } } }

        C 1 Reply Last reply
        0
        • A Angelinna

          This is where I started from, thanks. ............................................................. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TwosComplement { public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int i = 0; i < 8; i++) { bitstring[i] = 0;} twoc_output.Text = "enter an integer"; decimal_output.Text = "enter a length 8 bitstring"; } private int[] bitstring = new int[8]; private int nDecimal; //strings for reading the bitstring in and out private string instring, outstring; private string nextbit; //helper function for flipping bits in the bitstring private int negate(int x) { if (x == 1) return 0; return 1; } //method to convert decimal stored in "nDecimal" //to two's complement representation, stored in array "bitstring" private void decimal_to_twoc( ) { int nDecimal = Convert.ToInt32(decimal_input.Text); string numBits = Convert.ToString(decimal_output.Text); int carry = 1; int flag = 0; if (flag==0) { for (int i = 0; i < 8; i++) bitstring[i] = bitstring[i] + 2; bitstring[nDecimal] = carry; carry = 0; if (bitstring[nDecimal] > 1) { bitstring[nDecimal] = 0; carry = 1; } } }

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          OK, so your first issue is that the code, when it works, will only work once. Because you only reset the array once. Or is it meant to be cumulative ?

          Angelinna wrote:

          private int negate(int x) { if (x == 1) return 0; return 1; }

          Not sure that you call this, but is it really worth writing a function for ? How about just writing this: return 1 - x; and 1-x is something you can just code inline instead of making a call, surely ? I'd have expected you'd need to use the & operator to work out what bits are set in the original number, what you're doing here seems to me to be doomed to fail. bitstring[nDecimal] = carry; This will just crash if nDecimal > 7 and won't ever do anything useful.

          Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

          A 1 Reply Last reply
          0
          • C Christian Graus

            OK, so your first issue is that the code, when it works, will only work once. Because you only reset the array once. Or is it meant to be cumulative ?

            Angelinna wrote:

            private int negate(int x) { if (x == 1) return 0; return 1; }

            Not sure that you call this, but is it really worth writing a function for ? How about just writing this: return 1 - x; and 1-x is something you can just code inline instead of making a call, surely ? I'd have expected you'd need to use the & operator to work out what bits are set in the original number, what you're doing here seems to me to be doomed to fail. bitstring[nDecimal] = carry; This will just crash if nDecimal > 7 and won't ever do anything useful.

            Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

            A Offline
            A Offline
            Angelinna
            wrote on last edited by
            #5

            Ya , not really sure of what am doing. Would like the code to work for both positive and negative integers and should take care of any "out of range" errors....... Otherwise would appreciate tips or web where I may read more on this. Thanks.

            C 1 Reply Last reply
            0
            • A Angelinna

              Ya , not really sure of what am doing. Would like the code to work for both positive and negative integers and should take care of any "out of range" errors....... Otherwise would appreciate tips or web where I may read more on this. Thanks.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              http://en.wikipedia.org/wiki/Two's_complement[^] reads to me like a two's complement is the same as !x + 1.

              Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

              G 1 Reply Last reply
              0
              • C Christian Graus

                http://en.wikipedia.org/wiki/Two's_complement[^] reads to me like a two's complement is the same as !x + 1.

                Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Christian Graus wrote:

                reads to me like a two's complement is the same as !x + 1.

                Signed numbers are stored as two's complement, so the easiest way to get it is to cast a signed number to an unsigned number: int negAnswer = -42; uint twosComplement = (uint)negAnswer;

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups