Search code examples
c#bituint

How to convert uint to bool array?


for example

uint <- 1

I want to get

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

if

uint <- 8

get it

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

Just format by bit , How can I do?


Solution

  • using System;
    using System.Collections.Generic;
                        
    public class Program
    {
        public static void Main()
        {
            uint x = 1;
            List<bool> result = new List<bool>();
            for(int i = 0; i < 32; ++i)
            {
                bool isBitSet = ((x >> i) & 0x01) > 0;
                result.Add(isBitSet);
            }       
        }
    }
    

    Note that this will push the lsbit first.