28/10/13 19:33
tuttodiMC
Salve a tutti. Come da titolo ho creato una struttura in c# per lavorare più facilmente con i numeri binari. Ho ancora qualche dubbio perché è il primo tipo di valore che creo. Mettiamo che io creo una variabile di tipo bin, ovvero del tipo binario creato da me:
e poi voglio utilizzare il suo valore per una somma (ho eseguito l'overload dell'addizione):
come fa la variabile numeroBinario a restituire il suo valore? Cosa intendo dire: mettiamo che io vada a sommare due interi. Io so che l'intero ha il valore nello stack e viene richiamato quando faccio l'addizione. Come faccio io a far richiamare il valore che ottengo dall'inizializzazione con il costruttore? Io ho creato una variabile risultato a cui si accede tramite la proprietà relativa, ma come faccio negli esempi precedenti a far restituire il numero che mi serve, sapendo che il costruttore non restituisce neanche void?
Di seguito c'è la struttura nella sua interezza:
Spero di essere stato chiaro. Grazie in anticipo delle risposte.
... bin numeroBinario = new bin(34); ...
e poi voglio utilizzare il suo valore per una somma (ho eseguito l'overload dell'addizione):
... bin nuovoNumero = numeroBinario + 3; ...
come fa la variabile numeroBinario a restituire il suo valore? Cosa intendo dire: mettiamo che io vada a sommare due interi. Io so che l'intero ha il valore nello stack e viene richiamato quando faccio l'addizione. Come faccio io a far richiamare il valore che ottengo dall'inizializzazione con il costruttore? Io ho creato una variabile risultato a cui si accede tramite la proprietà relativa, ma come faccio negli esempi precedenti a far restituire il numero che mi serve, sapendo che il costruttore non restituisce neanche void?
Di seguito c'è la struttura nella sua interezza:
namespace BinaryData { struct bin { private static int risultato; private static int originalNumber; public int this [int index] //DICHIARO L'INDICIZZATORE PER MODIFICARE I SINGOLI BIT { get { int? ritorno = null; // CREO LA VARIABILE CHE CONTERRà IL RISULTATO bool inutile = (risultato & (1 << index)) != 0; // VERIFICO CHE IL BIT SIA DIVERSO DA 0 if (inutile == true) ritorno = 1; else // SE IL BIT TORNA TRUE IL RISULTATO SARà 1 ALTRIMENTI SARà 0 ritorno = 0; return ritorno; } set { if (value == 0) risultato &= ~(1 << index); else if (value == 1) risultato != (1 << index); } } public int OriginalNumber { get { return this.originalNumber; } } public int Result { get { return this.risultato; } } public bin(int numero) //COSTRUTTORE { originalNumber = numero; int risultato = ConvertToBin(numero); } public static bin operator +(bin lhs, bin rhs) { string lhsString = ConvertToBin(lhs.Result).ToString(), rhsString = ConvertToBin(rhs.Result).ToString(); string temp = ""; if (lhsString.Length > rhsString.Length) { int diff = lhsString.Length - rhsString.Length; int minore = "rhs"; } else if (rhsString.Length > lhsString.Length) { int diff = rhsString.Length - lhsString.Length; string minore = "lhs"; } for (; diff > 0; diff--) { temp += "0"; } switch (minore) { case "rhs": temp += rhsString; rhsString = temp; break; case "lhs": temp += lhsString; lhsString = temp; break; } string a = b = c = ""; bool rip = false; string result = ""; for (int i = minore.Length - 1; i >= 0; i--) { string s1 = lhsString.Substring(i, 1), s2 = rhsString.Substring(i, 1); switch (s1 && s2) { case "0" && "0": if (rip == true) result += "1"; else result += "0"; rip = false; break; case ("0" && "1") || ("1" && "0"): result += 0; if (rip == true) { result += "0"; rip = true; } else { result += "1"; rip = false; } break; case "1" && "1": if (rip == true) result += "1"; else result += "0"; rip = true; break; } } } public static bin operator +(bin lhs, int rhs) { bin newbin = new bin(rhs); return lhs + newbin; } public static bin operator +(int lhs, bin rhs) { bin newbin = new bin(lhs); return newbin + rhs; } public static bin Invert(bin op) { bin uno = new bin(1); return ~(op.Result) + uno; } } }
Spero di essere stato chiaro. Grazie in anticipo delle risposte.
aaa