How to get bits of a float value in C#

1 Answer

0 votes
using System;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        float value = 3.14f;
        
        var bytes = BitConverter.GetBytes(value)
                    .Reverse()
                    .Select(x => Convert.ToString(x, 2))
                    .Select(x => x.PadLeft(8, '0'))
                    .Aggregate("0b", (a, b) => a + "_" + b);

         
        Console.WriteLine(bytes);
    }
}
  
  
// https://www.h-schmidt.net/FloatConverter/IEEE754.html
  
  
/*
run:
    
0b_01000000_01001000_11110101_11000011
    
*/

 



answered Sep 3, 2023 by avibootz

Related questions

1 answer 159 views
2 answers 208 views
208 views asked May 22, 2024 by avibootz
1 answer 109 views
1 answer 130 views
1 answer 162 views
162 views asked Jan 12, 2017 by avibootz
...