-3

I have something like int i=10 it could be i=1000 or i=10400233

I like to store each bit from any number stored in int i=10//,etc. in an array. Is it possible in C

I just don't get it how can we find the relationship between bits and int variable with a number stored in it

example inputs

int i=10; // I want int array[]={1,0,1,0};

int i=15; // I want int array[] ={1,1,1,1};

int i=21 // I want int array[] ={1,0,1,0,1};

user786
  • 3,902
  • 4
  • 40
  • 72
  • 2
    Bits with value `0` are bits too, so all `int` will have the same number of bits (in a given compiled binary). – Joachim Sauer Apr 26 '21 at 07:50
  • Can you please rephrase and/or clarify the question as it is not clear. Perhaps give the result of some example inputs. But in general to extract bit X would be `(i >> X) & 1` – kaylum Apr 26 '21 at 07:53
  • @JoachimSauer do u mean its simply not possible because there is no way of finding how many bits represent a number. what if I tell on my system `printf("%d\n",sizeof(int));` prints `4` – user786 Apr 26 '21 at 07:53
  • 3
    I think the first comment is saying that the number of bits for every `int` value is the same. It is `sizeof(int)*8`. – kaylum Apr 26 '21 at 07:56
  • @kaylum I have updated my question with test inputs – user786 Apr 26 '21 at 08:00
  • 1
    So you want all significant bits avoiding leading zeroes? – Jean-Baptiste Yunès Apr 26 '21 at 08:01
  • @Jean-BaptisteYunès I want bits that can represent a decimal number. – user786 Apr 26 '21 at 08:02
  • Does this answer your question? [Find most significant set bit in a long](https://stackoverflow.com/questions/18939998/find-most-significant-set-bit-in-a-long) – kaylum Apr 26 '21 at 08:03
  • So you have multiple questions in this post. That duplicate shows how to find the most significant bit. You can then use bit shift and mask to get each bit starting from the most significant to the 0-th bit. Give it a try and come back to show your code if you have any specific difficulty. – kaylum Apr 26 '21 at 08:04
  • Kaylum gave you a way to extract a bit from an `int`, and to obtain the number of bits in an `int`. That is sufficient. Try to write some code, now. – Jean-Baptiste Yunès Apr 26 '21 at 08:06
  • what I can say it I need some upper bound on number of bits. If my int is 4 byte long than it worth trying 32 bits and do `int j=i>>31 & 0b1;` and `int j=i>>30 & 0b1;` and `int j=i>>29 & 0b1;`,.....`int j=i>>1 & 0b1;` – user786 Apr 26 '21 at 08:20
  • @Jean-BaptisteYunès can u please look at my answer – user786 Apr 26 '21 at 08:26

2 Answers2

1

I just don't get it how can we find the relationship between bits and int variable with a number stored in it

First it's important to understand that the number of bits used to store an int is a fixed number on the specific system being used. Different systems may use a different number of bits.

BUT ... The number of bits doesn't depend on the value held by the int. The number of bits used to store an int is a system-specific constant.

You can fid the number of bits like:

printf("int is %zu chars and each char is %d bits so in total %zu bits\n", 
       sizeof(int),              // Chars per int
       CHAR_BIT,                 // Bits per char
       sizeof(int) * CHAR_BIT);  // Bits per int

On my system I get:

int is 4 chars and each char is 8 bits so in total 32 bits

Once you have calculated the number of bits, you can create an array of that size. Either a VLA (variable length array) or a dynamic allocated array (i.e. using malloc).

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

This is how its done when sizeof(int) is 4 on my system

   #define CHAR_BIT_C 8
    
    int main()
    {
        int i=21;
        
        int t[32];
    
        for(int x=CHAR_BIT_C*sizeof(int)-1;x>=0;x--)
        {
            *(t+x)=i>>x&0b1;
        }
        for(int x=CHAR_BIT_C*sizeof(int)-1;x>=0;x--){
            printf("%d ",*(t+x));
        }

        return 0;        
    }
user786
  • 3,902
  • 4
  • 40
  • 72
  • Why do you have the variable `j`? -- Please raise the warning level of your compiler to the maximum and handle all warnings and errors. -- Use some blanks to make your source more readable. -- `void main()` is not one of the standard compliant main functions. – the busybee Apr 26 '21 at 08:45
  • @thebusybee how to raise the warning level of gcc compiler to the maximum? And how to remove it – user786 Apr 26 '21 at 09:02
  • Well, your first stop should always be the documentation. It is available by a simple web search and in different formats, even in PDF. Additional tutorials are widespread. -- Anyway, use at least `-Wall -Wextra` as options to the compile command line. Depending on your IDE and the type of project there might be a dialog for this. – the busybee Apr 26 '21 at 11:09