Ajay Chauhan
Ajay Chauhan

Reputation: 91

How do I identify the size of a byte on any platform?

The sizeof(data_type) operator returns the number of bytes and not octets, so the size of a byte may not be 8 bits. How can one identify the size of a byte in bits on any platform?

Upvotes: 1

Views: 153

Answers (2)

user1952500
user1952500

Reputation: 6771

A byte is commonly 8 bits independent of the platform (edited based on comment below). Else you could do:

  1. Set a byte (or an int) to -1.
  2. Count the number of bits in the above variable (and divide by sizeof(int), if you used an int).

Upvotes: 0

user1969104
user1969104

Reputation: 2420

I think you can do sizeof(type) * CHAR_BIT to determine the number of bits. Include limits.h for the definition of CHAR_BIT.

Upvotes: 4

Related Questions