Autor Beitrag
Rakeem
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Do 12.02.09 23:39 
Hi erst mal,
Ich wollte ein programm machen das von negativen dezimal nach binär umrechnet.
aber irgend wie krieg ich es nicht richtig gebacken.
Hier der sourcecode

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
using System;

namespace ConsoleApplication4
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      int i,neg,zahl;

      Console.Write ("Bitte geben Sie eine Zahl an :");
      zahl = Convert.ToInt32 (Console.ReadLine ());

      for (i=0;i<=16;i++)
      {
        zahl = (zahl +1);
        neg = zahl/2;
        Console.Write (neg);
        Console.Write ("/2 = ");
        Console.Write (zahl);
        Console.Write (" Rest ");
        Console.WriteLine(zahl %2);

        

        if (zahl %2 == 0)
        {
          Console.Write("1");
        }
        else 
        {
          Console.Write("0");
        }
        zahl=zahl/2;
      }
    }
  }
}


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Do 12.02.2009 um 23:01
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 12.02.09 23:40 
Könntest Du noch eine Fehlerbeschreibung nachreichen? "Nicht gebacken" kann viel bedeuten ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Rakeem Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Do 12.02.09 23:45 
hm ja ich habe halt auch von positiven zahlen gemacht und es hingekrigt
das wenn ich zum beispiel 5 eingebe er 101 ausgibt
ja wenn ich halt -5 eingebe muss das bei raus kommen 11111111111111111111111111111011
aber die zahl die da raus kommt ist ne andere.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 12.02.09 23:55 
Wie sieht denn dein Algorithmus für positive Zahlen aus? Aus Deinem für negative Zahlen werde ich irgendwie nicht schlau.

Du kannst ja auch einfach die negative Zahl mit -1 multiplizieren (dann haste die positive), das in Binär konvertieren und dann 'n Minuszeichen davor setzen ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Rakeem Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Do 12.02.09 23:56 
Der ist so
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
using System;

namespace Salvatore_Gabriello
{

    class Class1
    {

        [STAThread]
        static void Main(string[] args)
        {
            int zahl, i, Erg;

            Console.Write("Bitte geben Sie die gewünschte Zahl ein: ");
            zahl = Convert.ToInt32(Console.ReadLine());

            if (zahl <= 0)
            {
                Console.WriteLine("Bitte geben Sie eine postive Zahl ein!");
            }
            else
            {
                for (i = 0; i <= 256; i++)
                {
                    Erg = zahl / 2;

                    Console.Write(zahl % 2);

                    zahl = zahl / 2;
                    if (zahl == 0)
                    {
                        i = 256;
                    }
                }
                Console.WriteLine(" ");
            }
        }
    }
}


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19340
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 13.02.09 00:04 
user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
Du kannst ja auch einfach die negative Zahl mit -1 multiplizieren (dann haste die positive), das in Binär konvertieren und dann 'n Minuszeichen davor setzen ;-)
Nein, laut dem Beispiel (-5 --> 11111111111111111111111111111011) geht das nicht, denn es geht danach zu urteilen nicht um die normale Schreibweise sondern um die Zweierkomplementdarstellung im PC:
de.wikipedia.org/wiki/Zweierkomplement

Davon sehe ich allerdings nichts im Quelltext. :nixweiss:
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 13.02.09 00:09 
user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:
user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
Du kannst ja auch einfach die negative Zahl mit -1 multiplizieren (dann haste die positive), das in Binär konvertieren und dann 'n Minuszeichen davor setzen ;-)
Nein, laut dem Beispiel (-5 --> 11111111111111111111111111111011) geht das nicht, denn es geht danach zu urteilen nicht um die normale Schreibweise sondern um die Zweierkomplementdarstellung im PC:
de.wikipedia.org/wiki/Zweierkomplement

Ach, war mich nicht aufgefallen ;-) <-- Smily da!

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Rakeem Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 27



BeitragVerfasst: Fr 13.02.09 00:22 
ok danke für die hilfe habs jetzt hingekrigt.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 13.02.09 00:23 
Dann verrat doch bitte auch, wie Du das gemacht hast, falls mal jemand ein ähnliches Problem hat :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".