"Extension GetEnumerator() Example"

// Add a GetEnumerator to UInt16 that iterates through every bit (from MSB to LSB)
public static class UInt16Extensions {
	public static IEnumerator<bool> GetEnumerator(this UInt16 @this) {
		for (var i = (sizeof(UInt16) * 8) - 1; i >= 0; --i) {
			yield return (@this & (1 << i)) != 0;
		}
	}
}

// Usage example:
// This program writes "1100001100001111" to the console
ushort u = (ushort) 0b1100_0011_0000_1111U;
foreach (var bit in u) {
	Console.Write(bit ? 1 : 0); 
}


Code snippet taken from "Complete C# Quick Reference - C# 9".