/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs
36 строк
1 KB
Genevieve Warren
Add remarks for core types (#38970)
05 янв 2024, 22:14
Не верифицирован
05 янв 2024, 22:14
71d1377
Код
Авторство
О чём код?
// <Snippet1> using System; public class Example1 { public static void Main() { bool[] flags = { true, false }; foreach (var flag in flags) { // Get binary representation of flag. Byte value = BitConverter.GetBytes(flag)[0]; Console.WriteLine("Original value: {0}", flag); Console.WriteLine("Binary value: {0} ({1})", value, GetBinaryString(value)); // Restore the flag from its binary representation. bool newFlag = BitConverter.ToBoolean(new Byte[] { value }, 0); Console.WriteLine("Restored value: {0}\n", flag); } } private static string GetBinaryString(Byte value) { string retVal = Convert.ToString(value, 2); return new string('0', 8 - retVal.Length) + retVal; } } // The example displays the following output: // Original value: True // Binary value: 1 (00000001) // Restored value: True // // Original value: False // Binary value: 0 (00000000) // Restored value: False // </Snippet1>