Last active
May 2, 2020 23:31
-
-
Save ThiagoBarradas/d8a981fbd48989fd612c156dc983e3e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Por padrão o enum herda de int (Int32) | |
// 4.294.967.296 valores diferentes | |
// ou seja -2.147.483.648 até 2.147.483.647 | |
// como tem 32 bits, para Flags, pode-se ter no máximo 32 valores diferentes | |
[Flags] | |
public enum Cor | |
{ | |
Vermelho = 1, // 1 << 0 | |
Verde = 2, // 1 << 1 | |
Azul = 4, // 1 << 2 | |
// ... | |
Cinza = 536_870_912, // 1 << 29 | |
Preto = 1_073_741_824, // 1 << 30 | |
Branco = -2_147_483_648 // 1 << 31 | |
} | |
Cor cores = Cor.Vermelho | Cor.Cinza | Cor.Branco | Cor.Preto; // (1 + 536870912 + 1073741824 - 2147483648) | |
var binarioTexto = Convert.ToString((int) cores, 2); // 11100000000000000000000000000001 | |
// |||_Cinza Azul_||| | |
// ||_Preto Verde_|| | |
// |_Branco Vermelho_| | |
var decimalInt = (int) cores; // -536870911 | |
// Você pode alterar o tipo que o enumerador está herdando | |
// Por exemplo para uint (unsigned int) (Int32 sem utilizar um bit para o sinal, ou, UInt32) | |
// 4.294.967.296 valores diferentes | |
// ou seja 0 até 4.294.967.295 | |
// continuamos com 32 bits, para Flags, pode-se ter no máximo 32 valores diferentes | |
[Flags] | |
public enum Cor : uint | |
{ | |
Vermelho = 1, // 1 << 0 | |
Verde = 2, // 1 << 1 | |
Azul = 4, // 1 << 2 | |
// ... | |
Cinza = 536_870_912, // 1 << 29 | |
Preto = 1_073_741_824, // 1 << 30 | |
Branco = 2_147_483_648 // 1 << 31 | |
} | |
Cor cores = Cor.Vermelho | Cor.Cinza | Cor.Branco | Cor.Preto; // (1 + 536870912 + 1073741824 + 2147483648) | |
var binarioTexto = Convert.ToString((uint) cores, 2); // 11100000000000000000000000000001 | |
// |||_Cinza Azul_||| | |
// ||_Preto Verde_|| | |
// |_Branco Vermelho_| | |
var decimalUInt = (uint) cores; // 3758096385 | |
// Alterando para long ou ulong (Int64) podemos ter 18.446.744.073.709.551.616 valores diferentes | |
// Para long -9.223.372.036.854.775.808 a 9.223.372.036.854.775.807 | |
// Para ulong de 0 até 18.446.744.073.709.551.615 | |
// agora com 64 bts, para Flags, pode-se ter no máximo 64 valores diferentes | |
[Flags] | |
public enum Cor : long | |
{ | |
Vermelho = 1 << 0, | |
Verde = 1 << 1, | |
Azul = 1 << 2, | |
// ... | |
Cinza = 1 << 61, // 2_305_843_009_213_693_952 | |
Preto = 1 << 62, // 4_611_686_018_427_387_904 | |
Branco = 1 << 63 // -9_223_372_036_854_775_808 ou 9_223_372_036_854_775_808 para ulong | |
} | |
Cor cores = Cor.Vermelho | Cor.Cinza | Cor.Branco | Cor.Preto; // (1 + 2305843009213693952 + 4611686018427387904 - 9223372036854775808) | |
var binarioTexto = Convert.ToString((long) cores, 2); // 1110000000000000000000000000000000000000000000000000000000000001 | |
// |||_Cinza Azul_||| | |
// ||_Preto Verde_|| | |
// |_Branco Vermelho_| | |
var decimalLong = (long) cores; // -2305843009213693951 ou 16140901064495857665 se for ulong |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment