-1

I want to represent numbers as a 15 bit wide field. So, for example:

Number        15 bit wide filed representation of the number
 0            000000000000000 /*15 bits*/

'a'           000000001100001 /*15 bits*/

'b'           000000001100010 /*15 bits*/

 4            000000000000100 /*15 bits*/

If the number can be represented in a smaller amount of bits, 0s will precede it. I was thinking about bit fields, however when I tried doing this:

#include <stdio.h>
int main()
{
    typedef struct 
    {
        int a : 15;
    }A;
    A b;
    b.a = 0;
   printf("a is %d \n",a.b); 
   return 0;
}

I got this output:

0

Instead of:

000000000000000

However, I'm not talking just about printing a number (I'm not interested in %15 or anything similar). I want 0s preceding any number that can be represented in a smaller amount of bits, in any of the operations I do, not just printing.

How can I achieve this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Tree
  • 145
  • 1
  • 13
  • It's kind of unclear. Do you just want to print a 15-bit number in binary format? If so, you'll have to do some string manipulation. There isn't a library utility for what you ask. – DeiDei Mar 11 '17 at 16:45
  • 1
    Not clear what your problem is. There are always that many digits stored you specify for a bitfield. For output: how about reading the man-page of the functions you use? The documenation of `printf` is not only available in a secret monastry in the Himalaya at midnight on a certain day once per century. – too honest for this site Mar 11 '17 at 16:45
  • @ DeiDei No, as I wrote, I don't want to just print. Thanks. – Tree Mar 11 '17 at 16:46
  • @Tree, are you looking for something like `string binary = bitset<15>(num).to_string();`? – abhishek_naik Mar 11 '17 at 16:47
  • @Tree: You should learn the basics about internal representation of intgers. – too honest for this site Mar 11 '17 at 16:47
  • @abhishek_naik: Hardly. That is not C. And the bitrfield serves the same purpose. – too honest for this site Mar 11 '17 at 16:47
  • Yeah, @Olaf; just asking the OP if this is *something like* what he wants. – abhishek_naik Mar 11 '17 at 16:48
  • Maybe you want to use [bit arrays](http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/bit-array.html)? – Priyank Mar 11 '17 at 16:49
  • I guess, the thing is, I'm not sure whether or not a 0s will precede a number that can be represented in a smaller amount of bits. For example, say I'm doing this: #include int main() { typedef struct { int a :15; }A; A b[4]; b.a[0] = 0; return 0; } When I'll print the value of b.a[0] for example, will I get 000000000000000? Thanks! – Tree Mar 11 '17 at 16:59
  • I guess I am, not quite sure. Thanks@abhishek_naik – Tree Mar 11 '17 at 17:01
  • @Tree If you're not talking about printing, then there are no digits and your question is meaningless. `b.a[0]` is an error because `b` is an array, not a struct. – melpomene Mar 11 '17 at 17:03
  • 1
    Excuse me, but my question is not meaningless. Anyway, what do you mean by "there are no digits"? @melpomene – Tree Mar 11 '17 at 17:05

1 Answers1

0

You have to go over every bit separately, like this:

#include <stdio.h>

#define WIDTH 15

// print the m-th bit (m from 1..16 or 32) of n
void print_bit(n, m)
{
    printf("%d", n & (1 << (m - 1)));
}

typedef struct
{
    int a : WIDTH;
} A;

int main()
{
    A b;
    b.a = 0;

    int i;
    for(i = 1; i <= WIDTH; ++i)
        print_bit(b.a, i);
    return 0;
}

Output:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
000000000000000

Read more in How do I print one bit?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305