0
0
C Sharp (C#)programming~10 mins

Flags attribute and bitwise enums in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an enum with the Flags attribute.

C Sharp (C#)
[[1]]
public enum Days
{
    None = 0,
    Sunday = 1,
    Monday = 2
}
Drag options to blanks, or click blank then click option'
ASerializable
BFlags
CObsolete
DDataContract
Attempts:
3 left
💡 Hint
Common Mistakes
Using an unrelated attribute like Serializable instead of Flags.
Forgetting to add the attribute entirely.
2fill in blank
medium

Complete the code to define enum values as powers of two for bitwise combination.

C Sharp (C#)
public enum Permissions
{
    Read = 1,
    Write = [1],
    Execute = 4
}
Drag options to blanks, or click blank then click option'
A3
B5
C2
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using values that are not powers of two, which breaks bitwise uniqueness.
3fill in blank
hard

Fix the error in combining flags using bitwise OR operator.

C Sharp (C#)
Permissions userPermissions = Permissions.Read [1] Permissions.Write;
Drag options to blanks, or click blank then click option'
A+
B^
C&
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which adds values but is not correct for flags.
Using '&' which is bitwise AND and only keeps common bits.
4fill in blank
hard

Fill both blanks to check if a flag is set using bitwise AND and compare to the flag.

C Sharp (C#)
if ((userPermissions [1] Permissions.Write) [2] Permissions.Write)
{
    Console.WriteLine("Write permission granted.");
}
Drag options to blanks, or click blank then click option'
A&
B==
C!=
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' instead of '&' in the check.
Using '!=' instead of '==' for comparison.
5fill in blank
hard

Fill all three blanks to define a Flags enum, combine flags, and check if a flag is set.

C Sharp (C#)
[[1]]
public enum Access
{
    None = 0,
    Read = 1,
    Write = 2
}

Access accessRights = Access.Read [2] Access.Write;

bool canWrite = (accessRights [3] Access.Write) == Access.Write;
Drag options to blanks, or click blank then click option'
AFlags
B|
C&
DSerializable
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute like Serializable.
Using '+' instead of '|' to combine flags.
Using '|' instead of '&' to check flags.