1

Here's a couple of thoughts.I'm learning so there might be mistake(s) and even missing some basics.

  1. sizeof operator returns number of bytes.
  2. number of bits in byte is not constant value(correct me but it's number of bits char has).
  3. I want to know how many bits variable occupies, and sizeof won't tell me that without making assumptions about number of bits in char.

So I came up with this piece of (probably unnecessary) code:

#include <stdio.h>
#include <math.h>
#include <limits.h>

int main(void)
{
  double bits;

  bits=sizeof(int)*log10(UCHAR_MAX+1)/log10(2);

  printf("Bits = %lf\n", bits);

  return 0;
}

Is there easier (standard) way to check how many bits given type occupies?
CHAR_BIT * sizeof(type) will do the job, but is there standard one argument macro/function that does that for me?
Someone with better mathematical background could check if my code will be always giving correct answers.

Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
zubergu
  • 3,646
  • 3
  • 25
  • 38
  • 1
    possible duplicate of [Is char guaranteed to be exactly 8-bit long in C?](http://stackoverflow.com/questions/881894/is-char-guaranteed-to-be-exactly-8-bit-long-in-c) – drahnr Oct 08 '13 at 09:53
  • @drahnr it is not, it's completely different question. I know how char/byte can have different size. – zubergu Oct 08 '13 at 10:14

4 Answers4

3
const size_t numberOfBits = CHAR_BIT * sizeof(type);
unwind
  • 391,730
  • 64
  • 469
  • 606
user694733
  • 15,208
  • 2
  • 42
  • 68
2

If you want to check for how many bits the machine actually used for a single char use the CHAR_BIT macro (note that sizeof (char) will always return one by definition though the actual allocation size per char may be still taller (even 32bits) though wasteful)


I am not aware of any predefined macro, but

#define REAL_BITS(type) (CHAR_BIT*sizeof(type))

should suffice

drahnr
  • 6,782
  • 5
  • 48
  • 75
  • I missed that one from limits.h. Now that I checked I think that is CHAR_BIT. Is there single argument standard defined macro/fuction that takes data type and returns number of bits?(Put that in an answer with correction and that will be something I can accept). – zubergu Oct 08 '13 at 10:12
1

Using CHAR_BIT (defined in limits.h) should do it.

CHAR_BIT * sizeof(something)

I have checked a number of *nix platforms and they are all 8 bits in a byte, but I guess it may vary on some stranger platforms.

Daniel
  • 4,797
  • 2
  • 23
  • 30
0

char data type is a byte. a int is 4bytes (dword). short is 2bytes (word). so, how many bits? just sizeof(the_char)*8. when you want to convert bytes to bits just bits_number=bytes_number*8 :)

int3
  • 658
  • 1
  • 5
  • 21
  • I think char will be always 8bit. even you are under 64bit system. What i know is char is a byte data type. 2 "chars" or 2 bytes can be a short integer (2bytes,16bits) etc... – int3 Oct 08 '13 at 10:28
  • In assembly when I push a byte (char) into stack it has 8bits. I can debug it and watch the stack. – int3 Oct 08 '13 at 10:32