Finde alle Bit (int-Zahl) (Schleifen)
Der Benutzer gibt eine vorzeichenlose Zahl ein und das Programm gibt aus, an welchen Stelle die Bits gesetzt sind.
z.B. Zahl: 3
Bit gesetzt: 1. Stelle
Bit gesetzt: 2. Stelle
2 Kommentare
6 Lösung(en)
def print_bit(n):
for i, bit in enumerate("{:b}".format(n)):
if bit == "1":
print("Bit gesetzt: {}. Stelle".format(i+1))
inp = None
while True:
try:
inp = input("Bitte gib die Zahl ein: ")
print_bit(int(inp))
except:
if inp == "":
break
print("""Es ist ein Fehler aufgetreten. Bitte überprüfe deine Eingabe.
Falls du das Programm beenden möchtest gib einfach nichts ein""")
Lösung von: Karl Welzel ()
package ch.programmieraufgaben.datentyp;
import java.util.Scanner;
/**
* Programmieraufgaben finde alle Bits.
* @version 0.1 (Aug 16, 2014)
* @author Philipp Gressly Freimann
* (philipp.gressly@santis.ch)
*/
public class FindeAlleBit {
public static void main(String[] args) {
new FindeAlleBit().top();
}
void top() {
long zahl = einlesenLong("Bitte Zahl eingeben:");
int pos = 1;
while(zahl > 0) {
if(1 == (1 & zahl)) {
System.out.println("Bit an Positon " + pos + " ist gesetzt.");
}
zahl = zahl / 2;
pos = pos + 1;
}
}
long einlesenLong(String frage) {
String eingabe = einlesenString(frage);
return Long.parseLong(eingabe);
}
Scanner sc = new Scanner(System.in);
String einlesenString(String frage) {
String eingabe = "";
while("".equals(eingabe)) {
System.out.println(frage);
eingabe = sc.nextLine().trim();
}
return eingabe;
}
} // end of class FindeAlleBit
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
use strict;
printBits(3);
sub printBits {
my $count=0;
for(reverse split//,sprintf("%b",$_[0]))
{print "Bit gesetzt: @{[++$count]}. Stelle$/"x!!$_;}
}
Lösung von: Paul Scheinast (http://scheinast.eu)
using System;
namespace Finde_Alle_Bits {
class Program {
static void Main(string[] args) {
byte? eingabe = null;
while (eingabe == null) {
Console.Clear();
Console.Write("Byte>");
try { eingabe = Convert.ToByte(Console.ReadLine()); }
catch (FormatException) { }
catch (OverflowException) { }
}
Console.WriteLine();
for (byte stelle = 0, vergleichsByte = 1; vergleichsByte != 0; stelle++, vergleichsByte <<= 1)
if ((eingabe & vergleichsByte) == vergleichsByte)
Console.WriteLine("Bit gesetzt: {0}. Stelle", stelle);
Console.ReadKey(true);
}
}
}
Lösung von: Marcel Kapma ()
function listBits(num) {
var arr = [],
i = 0;
num = parseInt(num).toString(2);
for (i; i < num.length; i++) if (num.charAt(i) === "1") arr.push(i+1);
return arr;
}
console.log(listBits(prompt("Geben Sie eine Zahl ein (>1)")));
// lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
// Positionen sind 1-basiert und entgegen der Leserichtung
Console.Write("Bitte Zahl eingeben: ");
_ = int.TryParse(Console.ReadLine(), out var n);
// Variante 1
Convert.ToString(n, 2).Reverse().Select((x, y) => (num: x, pos: y + 1)).Where(x => x.num == '1').ToList().ForEach(x => Console.WriteLine($"Bit gesetzt an {x.pos}. Stelle."));
// Variante 2
var com = 1;
var pos = 1;
do {
if ((n & com) == com) Console.WriteLine($"Bit gesetzt an {pos}. Stelle.");
pos++;
} while ((com <<= 1) <= n);
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Kommentare (2)
Da in der Aufgabe nicht spezifiziert ist, was die Leserichtung von Zahlen sein soll, muss dies dem Programmierer überlassen werden.