Bitset examples from the FAQ (unsigned char?) 
Author Message
 Bitset examples from the FAQ (unsigned char?)

Steve posted some examples of bitset macros in the clc FAQ.  I also googled
back a bit and he posted a full example in which he uses this (and more)
macros to accomplish this task.

In his example, he packs the bits into arrays of char.  My question is, why
didn't he use unsigned char which is recommended for bit fiddling?  Or are
all the operations he's performing considered safe?

Rather than reposting the code, I'll just put the URL here:

http://www.*-*-*.com/ :comp.lang.c+bitnslots&hl=en&l...

Thanks,

-Clint



Mon, 19 Sep 2005 05:38:57 GMT  
 Bitset examples from the FAQ (unsigned char?)

Quote:

> In his example, he packs the bits into arrays of char.  My question is,
why
> didn't he use unsigned char which is recommended for bit fiddling?  Or are
> all the operations he's performing considered safe?

Steve goofed. unsigned char for arbitrary data, or else you may get a trap
representation.


Mon, 19 Sep 2005 06:58:44 GMT  
 Bitset examples from the FAQ (unsigned char?)

Quote:

> Steve posted some examples of bitset macros in the clc FAQ.  I also googled
> back a bit and he posted a full example in which he uses this (and more)
> macros to accomplish this task.

> In his example, he packs the bits into arrays of char.  My question is, why
> didn't he use unsigned char which is recommended for bit fiddling?  Or are
> all the operations he's performing considered safe?

    He should be using `unsigned char' rather than plain `char'.
For even more safety, the macro

        #define BITMASK(b) (1 << ((b) % CHAR_BIT))

should be written as

        #define BITMASK(b) (1u << ((b) % CHAR_BIT))

... so it will work correctly even when sizeof(int) is 1.

--



Mon, 19 Sep 2005 07:16:12 GMT  
 Bitset examples from the FAQ (unsigned char?)

Quote:

>    #define BITMASK(b) (1u << ((b) % CHAR_BIT))

> ... so it will work correctly even when sizeof(int) is 1.

Ahh, good catch.

So, is the FAQ going to get updated with this information?  Having to
double-check the FAQ with Google seems a bit clumsy :)

-Clint



Mon, 19 Sep 2005 16:39:43 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

2. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

3. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

4. bytes to unsigned char, unsigned short, unsigned int, ...

5. char, unsigned char, signed char

6. From unsigned int to unsigned char

7. unsigned char assignment to unsigned int.

8. Casting from unsigned char[] to unsigned short

9. How to convert unsigned long to unsigned char?

10. To convert unsigned char to unsigned short in VC++

11. unsigned/unsigned char

12. To convert unsigned char to unsigned short in VC++

 

 
Powered by phpBB® Forum Software