diku and huge numbers....
Goto page Previous  1, 2
 
Post new topic   Reply to topic    mudlab.org Forum Index -> Coding
View previous topic :: View next topic  
Author Message
Kaz



Joined: 05 Jun 2005
Posts: 24
Location: Hampshire, UK

PostPosted: Fri May 19, 2006 8:13 am    Post subject: Reply with quote

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
View user's profile Send private message
Author Message
eiz



Joined: 11 May 2005
Posts: 152
Location: Florida

PostPosted: Sat May 20, 2006 6:30 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Author Message
sm007h



Joined: 15 May 2005
Posts: 3

PostPosted: Tue Jun 06, 2006 8:36 am    Post subject: bitvectors and bitwise functions Reply with quote

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
View user's profile Send private message
Author Message
Tyche



Joined: 13 May 2005
Posts: 176
Location: Ohio, USA

PostPosted: Tue Jun 06, 2006 9:26 am    Post subject: Re: bitvectors and bitwise functions Reply with quote

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
View user's profile Send private message Visit poster's website
Author Message
elanthis



Joined: 13 Apr 2006
Posts: 10

PostPosted: Mon Jul 10, 2006 2:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    mudlab.org Forum Index -> Coding All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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

Powered by phpBB © 2001, 2002 phpBB Group
BBTech Template by © 2003-04 MDesign