-1

Iv'e look through this answer.

Can not seem to understand this line of code:

I'm trying to get as many cultures as i can.

CultureInfo[] cinfo = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

public enum CultureTypes
{
    NeutralCultures = 1,
    SpecificCultures = 2,
    InstalledWin32Cultures = 4,
    AllCultures = 7,
    UserCustomCulture = 8,
    ReplacementCultures = 16,
    [Obsolete("This value has been deprecated.  Please use other values in CultureTypes.")]
    WindowsOnlyCultures = 32,
    [Obsolete("This value has been deprecated.  Please use other values in CultureTypes.")]
    FrameworkCultures = 64,
}

does the tilde makes the constructor behave like this?:

CultureInfo[] cinfo = CultureInfo.GetCultures(CultureTypes.AllCultures | 
CultureTypes.NeutralCultures | 
CultureTypes.SpecificCultures | 
CultureTypes.InstalledWin32Cultures | 
CultureTypes.UserCustomCulture | 
CultureTypes.ReplacementCultures );
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91

2 Answers2

2

~ is the Bitwise complement operator as described here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-complement-operator

Please see Luceros' comment under Odeds answer to this question: Why not all countries are presented in CultureInfo.GetCultures()?, which explains that the Bitwise complement operator is used to state that Neutral Cultures should not be used in the result of the expression.

w0051977
  • 15,099
  • 32
  • 152
  • 329
2

This expression:

CultureTypes.AllCultures & ~CultureTypes.NeutralCultures

Is the equivalent (in natural language) of: "AllCultures except for NeutralCultures".

The bitwise complement operator ~ will negate the value of CultureTypes.NeutralCultures.

Let's do some bitwise math manually:

AllCultures            = 0111 // 1+2+4=7
NeutralCultures        = 0001 // 1
SpecificCultures       = 0010 // 2
InstalledWin32Cultures = 0100 // 4

So:

~NeutralCultures = 1110 // bits flipped

And:

AllCultures & ~NeutralCultures = 0111 & 1110

Which results in:

0111
1110
----
0110

0110 is 6 in decimal, which is equivalent to SpecificCultures | InstalledWin32Cultures

Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56