views:

96

answers:

2
+2  Q: 

What is CHAR_BIT?

From http://graphics.stanford.edu/~seander/bithacks.html:

int v;           // we want to find the absolute value of v
unsigned int r;  // the result goes here 
int const mask = v >> sizeof(int) * CHAR_BIT - 1;

r = (v + mask) ^ mask;
Patented variation:
r = (v ^ mask) - mask;

What is CHAR_BIT and how use it in programming languages? c++ or java

+6  A: 

CHAR_BIT is the number of bits in char. These days, almost all architectures use 8 bits per byte but it is not the case always. Some older machines used to have 7-bit byte.

AraK
Some DSPs have 10 or more bit-bytes.
Juri Robl
i have test on my machine and it use 7bit byte
C requires `CHAR_BIT>=8` and allows much larger values for DSPs which only have a single type size, often 32bit. POSIX requires `CHAR_BIT==8`. In general, you can assume any multi-user/multitasking server-oriented or interactive-use-oriented architecture with any chance of being connected to the internet or interchanging textual data with the outside world has `CHAR_BIT==8`.
R..
As an implication of other requirements C99 (in contrast to C89 as in R.'s comment) also defacto requires `CHAR_BIT==8`.
Jens Gustedt
@Jens: Are you referring to the issue of `unsigned char` being promoted to `unsigned int` rather than `int`?
caf
@caf: No, it is that C99 requires the types `int8_t` and `uint8_t` to exist. Thus there exists a type of width 8. Since `sizeof` any type must be compatible with `sizeof char` actually `sizeof int8_t` must be 1. So `CHAR_BIT == 8`. I have written up something around that obeservation here: https://gustedt.wordpress.com/2010/06/01/how-many-bits-has-a-byte/
Jens Gustedt
@Jens Gustedt: Please cite a section in the C99 spec. Of the exact-width integer types, the C99 spec says "These types are optional." (7.18.1.1/3) The minimum-width and fastest-width types are required, however.
jamesdlin
@Jens: As @jamesdlin says, the exact-width types are optional. The wording is: "Conversely, for each typedescribed herein that the implementation does not provide, `<stdint.h>` shall not declare that typedef name nor shall it define the associated macros. An implementation shall provide those types described as "required", but need not provide any of the others (described as "optional")." This is one good reason why you shouldn't use those types unless you actually, really *do* need an exact-width type.
caf
Jens Gustedt
@Jens Gustedt: POSIX is in ISO and C is in ISO. Version of POSIX has to always refer to the latest version of C standard on the moment of POSIX being accepted as a standard. POSIXv6/SUSv3 (2001) uses C99. SUSv2/earlier use C89. http://en.wikipedia.org/wiki/Single_UNIX_Specification
Dummy00001
A: 

You should be aware that this code depends on the implementation-defined behavior of right bitshift on signed types. gcc promises to always give the sane behavior (sign-bit-extension) but ISO C allows the implementation to zero-fill the upper bits.

One way around this problem:

#ifdef HAVE_SIGN_EXTENDING_BITSHIFT
int const mask = v >> sizeof(int) * CHAR_BIT - 1;
#else
int const mask = -((unsigned)v >> sizeof(int) * CHAR_BIT - 1);
#endif

Your Makefile or config.h etc. can define HAVE_SIGN_EXTENDING_BITSHIFT at build time depending on your platform.

R..