& is the bitwise logical and operator - Esegue l'operazione su 2 valori interi.
WHERE (sc.Attributes & 1) = 0
Il codice precedente controlla se sc.Attributes è un numero pari. Il che equivale a dire che il primo bit non è impostato.
A causa del nome della colonna però: "Attributi", quindi il valore "1" è probabilmente solo qualche bandiera che ha un significato esterno.
È comune utilizzare 1 cifra binaria per ogni bandiera memorizzata in un numero per attributi. Quindi per testare per il primo bit si usa sc.Attributes & 1, per testare per il secondo si usa sc.Attributes & 2, per testare il terzo si usa sc.Attributes & 4, per testare il quarto si usa sc. Attributi & 8, ...
La parte = 0 sta verificando per vedere se il primo bit NON è impostato.
Alcuni esempi binari: (== per mostrare il risultato dell'operazione)
//Check if the first bit is set, same as sc.Attributes&1
11111111 & 00000001 == 1
11111110 & 00000001 == 0
00000001 & 00000001 == 1
//Check if the third bit is set, same as sc.Attributes&4
11111111 & 00000100 == 1
11111011 & 00000100 == 0
00000100 & 00000100 == 1
fonte
2009-03-21 23:44:13
+1 Molto ben spiegato. –
@Andrew Hare: Grazie :) –
Grazie. Mi scuso per una domanda così semplice. Apprezzo davvero l'ottima risposta e il tempo impiegato. – Jimmymcnulty