Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallöli!

Wir müssten kurz auf die Schnelle wissen, wie man nach Eingabe einer Dezimalzahl (zB 14) diese in c-Prog. als eine Dualzahl (1110) ausgibt.

Danke

Da PhOeniX verneigt sich vor der C Kenntnis des Shadax und zieht seinen Beitrag beschähmt zurück!

<FONT COLOR="#a62a2a" SIZE="1">[ 14. Dezember 2001 14:57: Beitrag 3 mal editiert, zuletzt von --==PhOeniX==-- ]</font>

Vorsicht: neagtive Werte berücksichtige ich nicht

(bei negativen Werten wird eine `1' ausgegeben)



// reads a decimal value from stdin and prints it in binary form to stdout


#include <stdio.h>

#include <stdlib.h>


//------------------------------------------------------------

// Dec2Bin() - prints the given value in binary form to stdout

//------------------------------------------------------------

void Dec2Bin(int val)

{

  if(val > 1)

  {

    Dec2Bin(val >> 1);

  }

  fputc((val & 1) ? '1':'0', stdout);

}



//-------------------

// main()

//-------------------

int main(int argc, char *argv[])

{

  if(argc < 2)

  {

    fprintf(stderr, "Usage: %s <decimal value>\n", argv[0]);

    return 1;

  }


  Dec2Bin(atoi(argv[1]));

  return 0;

}


<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Zitat:</font><HR>Original erstellt von Klotzkopp:

<STRONG>

  

void Dec2Bin(int val)

{  

	for( int i=0; (2 << i) <= val; i++ );


	for( int j=i; j; j-- ) {

		fputc((val & ( 1<<j)) ? '1':'0', stdout);

	}

}

</STRONG>

<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Zitat:</font><HR>Original erstellt von ElektroUli:

<STRONG>Weiss nicht, ob ich das auf die Schnelle scharf genug bedenke, aber es kommt mir so vor, dass es Probleme gibt, wenn bei val das MSB gesetzt ist. Bei einem unsigned int mit 16 Bit laeuft i bis 14 und 2<<14 == 1000 0000 0000 0000. Das ist kleiner als als z. B. 1000 0000 0000 0001. Fuer alle i>14 ist wird 2<<i Null; und das ist ebenfalls kleiner als val. Ergo Endlosschleife. Oder?</STRONG>

Richtig.

Val hat zwar ein Vorzeichen. Wenn das MSB gesetzt ist, dann ist val negativ, und damit ist (2<<i)<=val schon für i == 0 falsch. Die Funktion tut gar nichts.

Allerdings ist das nur Schmerzverlagerungstherapie. Die Endlosschleife tritt auf, wenn das zweithöchste Bit gesetzt ist. Es war sogar noch ein anderer Fehler drin. Daher jetzt die korrigierte Fassung:

void Dec2Bin(int val)
{
bool fOneFound = false;
for( int i = sizeof(int) * 8 - 1; i >= 0; i-- ) {
if( !fOneFound && ( val & ( 1<<i ) ) ) fOneFound = true;
if( fOneFound || !i ) fputc((val & ( 1<<i)) ? '1':'0', stdout);
}
}[/code]

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.