Autor Beitrag
MrFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Sa 09.07.05 19:50 
Hi Leute,

Ich möchte ein Array[0..7] of Boolean, das 1 Byte repräsentiert in eine BYTE variable umwandeln

(um daraus wiederrum einen Buchstaben werden zu lassen)

Jetzt hab ich mir da schon so 'ne Funktion geschrieben, da funktioniert aber irgendwas nicht...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var wert:byte;
    bytes:Array[0..7of Boolean;

...

for i := 7 downto 0 do 
   begin
   wert := wert shl 1;
   if bytes[i] = true then wert := wert or 1 else wert := wert or 0;
   end;

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Sa 09.07.05 20:07 
Also, eigentlich sollte es gehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var wert:byte;  
    bytes:Array[0..7of Boolean;  

{...}

  wert:=0;
  for i := 7 downto 0 do begin
   wert:=wert shl 1;
   if bytes[i] then  Inc(Wert);
  end;

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.


Zuletzt bearbeitet von maxk am Sa 09.07.05 20:11, insgesamt 2-mal bearbeitet
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Sa 09.07.05 20:10 
@maxk, muss man da nicht der variable wert vorher 0 zugewiesen werden? fängt ja sonst vielleicht mit 5 etc. an.

_________________
"Leben ist gänzlich Bühne und Spiel; so lerne denn spielen
und entsage dem Ernst - oder erdulde das Leid." - Palladas von Alexandria


Zuletzt bearbeitet von ManuelGS am Sa 09.07.05 20:29, insgesamt 2-mal bearbeitet
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Sa 09.07.05 20:12 
Stimmt, ich hatte mich da gerade selber ein bisschen verwirrt. Aber nach 100mal editieren, glaube ich meine Endfassung gefunden zu haben ;)

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Sa 09.07.05 20:26 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var wert:byte;
    bytes:Array[0..7of Boolean;
    i:integer;

begin
bytes[0]:=true;
bytes[1]:=true;
bytes[2]:=true;
bytes[3]:=false;
bytes[4]:=false;
bytes[5]:=false;
bytes[6]:=true;
bytes[7]:=false;         //11100010b  - 226d *räusper*

  wert:=0;
  for i := 0 to 7 do begin
  wert:=wert shl 1;
   if bytes[i] then wert:=wert or 1;

  end;
showmessage(inttostr(wert));

_________________
"Leben ist gänzlich Bühne und Spiel; so lerne denn spielen
und entsage dem Ernst - oder erdulde das Leid." - Palladas von Alexandria


Zuletzt bearbeitet von ManuelGS am Sa 09.07.05 22:14, insgesamt 1-mal bearbeitet
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Sa 09.07.05 21:38 
11100010b ist aber 226d - und das ermittelt auch der Code ;)

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Sa 09.07.05 22:05 
262 ist eine sogenannte "Manuelzahl". Man berechnet sie, indem man die Ziffern einer beliebigen Zahl z nimmt, und in zufälliger Reihenfolge anordnet.
Hast du das etwa nicht gewusst? :wink:

lol

_________________
"Leben ist gänzlich Bühne und Spiel; so lerne denn spielen
und entsage dem Ernst - oder erdulde das Leid." - Palladas von Alexandria
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 09.07.05 22:53 
Leichtere Möglichkeit:

ausblenden Delphi-Quelltext
1:
type TBitSet = set of 0..7;					


Danach einfach Include(Bits, BitNumber); um das BitNumber-te Bit zu einer Zahl hinzuzufügen oder Exclude(Bits, BitNumber); um das BitNumber-te Bit aus einer Zahl zu entfernen.

In ein Byte wird das ganze dann mit ByteVal := Byte(TBitSet(Bits)); konvertiert.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: So 10.07.05 00:03 
user profile iconManuelGS hat folgendes geschrieben:
262 ist eine sogenannte "Manuelzahl". Man berechnet sie, indem man die Ziffern einer beliebigen Zahl z nimmt, und in zufälliger Reihenfolge anordnet.
Das steht in meiner nächsten Infoklausur ;)

@BenBE: Verblüffent einfach :autsch:

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: So 10.07.05 11:23 
Hab den Fehler selbst gefunden. Meine Funktion war völlig richtig, ich bin das ganze bloß in der falschen Richtung durchgegangen.

Trotzdem Danke für die zahlreichen "Anregungen"

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.