 |
 |
 |
 |
 |
Author |
Message |
Kaz

Joined: 05 Jun 2005 Posts: 24 Location: Hampshire, UK
|
Posted: Fri May 19, 2006 8:13 am Post subject: |
|
|
KaVir wrote: | Ah yes, sorry, I misread your example code. However 'unsigned char' is still preferable to 'char' for the same reasons. |
It's not defined by the C standard whether char is signed or unsigned. All platforms that I've coded for have it signed by default. GCC, however, at least has an option to make it unsigned: -funsigned-char.
In any case, it must be unsigned, or bitsets may work unpredictably with shift operators. This can be trivially observed here:
Code: |
#include <stdio.h>
int main()
{
char ch = -4;
printf("%d\n", ch >> 1);
}
|
which outputs -2, if char is signed. This indicates that the sign bit is sticky, and hasn't shifted at all. |
|
Back to top |
|
 |
|
 |
 |
 |
 |
 |
 |
 |
 |
Author |
Message |
eiz
Joined: 11 May 2005 Posts: 152 Location: Florida
|
Posted: Sat May 20, 2006 6:30 pm Post subject: |
|
|
Kjartan wrote: | eiz wrote: | Any modern compiler can use strength reduction to transform them into equivalent code anyway. |
It depends what CHAR_BIT is; if it's #define'd, yes, if it's "int CHAR_BIT = 8", then I think no.
I love that chunk of example code you linked to. Now that's some bit twiddling right there.
(And actually I don't know what "strength reduction" is, I'm assuming you just mean that the compiler will transform them into equivalent code. I looked it up on wikipedia and their definition didn't seem relevant, or if it is it's in some manner too subtle for me.) |
I don't have my copy of the standard handy, but I'm pretty sure CHAR_BIT is defined as a macro.
The definition of strength reduction on Wikipedia is a generalized version of the same concept. Transformations like turning a multiply into a series of shifts and adds, or replacing a constant division with a shift or constant multiply by reciprocal, are sometimes referred to as "weak strength reduction." It's actually a pretty interesting problem.
(Poor Kyuss, just asked a simple question about bit vectors and look where it got him!) |
|
Back to top |
|
 |
|
 |
 |
 |
 |
 |
 |
 |
 |
Author |
Message |
sm007h
Joined: 15 May 2005 Posts: 3
|
Posted: Tue Jun 06, 2006 8:36 am Post subject: bitvectors and bitwise functions |
|
|
I have always been weak at this, since I have had no formal training.
Can anyone direct me to any texts that can get me up to speed so I can actually understand what you guys were trying to do? |
|
Back to top |
|
 |
|
 |
 |
 |
 |
 |
 |
 |
 |
Author |
Message |
Tyche
Joined: 13 May 2005 Posts: 176 Location: Ohio, USA
|
Posted: Tue Jun 06, 2006 9:26 am Post subject: Re: bitvectors and bitwise functions |
|
|
sm007h wrote: | I have always been weak at this, since I have had no formal training.
Can anyone direct me to any texts that can get me up to speed so I can actually understand what you guys were trying to do? |
Bitwise Operations in C
Bit Twiddling Hacks |
|
Back to top |
|
 |
|
 |
 |
 |
 |
 |
 |
 |
 |
Author |
Message |
elanthis
Joined: 13 Apr 2006 Posts: 10
|
Posted: Mon Jul 10, 2006 2:23 pm Post subject: |
|
|
Quote: | It depends what CHAR_BIT is; if it's #define'd, yes, if it's "int CHAR_BIT = 8", then I think no. |
It's almost definitely a macro. However, if it's defined as a 'const int' then it will still be optimized just as if you had used a macro. That is, so long as the actual definition is in the translation unit, not just the declaration. |
|
Back to top |
|
 |
|
 |
 |
 |
 |
 |
 |
 |
 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
 |
 |
 |
 |
|
 |