Shifts Ahoy

I’m not sure I like this being valid. In my universe a bool is a bool is a bool is a bit.

unsigned char a, b, c, d ;
typedef unsigned UINT32 ;
UINT32 x ;

x = (a < < 24) | (b << 16) | (c < 8) | (d) ;[/sourcecode] "x is a shifted left 24 bits plus b shifted left 16 bits plus (is c less than 8?) plus d" Why can I shift an unsigned char 24 bits left? (Intel's compiler secretly casts to a signed int, regardless of the signedness of your input variable, GCC appears to be a bit smarter, Visual C seemed to be rather smart). Ok, I can, but why am I allowed to bitwise OR a bool? Nothing on any of the 3 compilers we use will elicit a peep out of a compiler. (c < 8) evaluates to a bool. It seems like it would make sense to restrict bools to logical OR. [sourcecode language='c++']bool a = true, b = false ; bool c = (a || b) ;[/sourcecode]


But that’s C(extended onto C++). Remember the binding virtues of C:

Don’t make a programmer pay for any operation he doesn’t explicitly call
Any situation not defined by the C language can be implemented as the compiler writer sees fit
Always give the programmer enough rope to hang himself

The results of bitwise ORing a boolean are not defined in C, therefor it’s up to the compiler writer. How would he choose to implement it? Well, since the bool is already stored as an integer then just OR it like an integer.

The same with shifting a char. Sure it’s a byte, but it’s loaded into a register for a shift operation. That register is always 4 bytes, so you would have to have the compiler check that the shift value was less that the size of a char before the operation and throw an error if the value was invalid. That’s work the program writer didn’t explicitly ask for, so C won’t.

All reasons to argue against C/C++ as a language, but it does have its own internal consistency.

Ah, but I’m talking about at the compiler stage. Especially in C++ where you are expecting strict typing, the compiler *ought* to be aware of the type of the LHS in a shift operation. I wasn’t suggesting runtime size checking.

IIRC the C++ standard says, that a bool can be converted into an int implicit. So the C++ compiler will simply convert the bool into an int (1 or 0) and then use the Bitoperations on it.

And a bool in C++ is not a bit. It is only defined in this way:
sizeof(bool) >= sizeof(char)


I did say “in my universe” :) There’s a bunch of work going on behind the scenes to ensure that bools [i]are[/i] ‘true’ or ‘false’, and not ’37’, etc.

I’ll bet there’d be a whole bunch of things you could catch if booleans lost some of their it’s actually stored as an unsigned int baby fat.

That’s why we have a type BOOL that is just a typecast of an unsigned int. That way, at least, we can just be honest about it. Of course, that does mean that a BOOL value can be non 1 or 0, but that’s ok, you should only be using logical operators (or bitmasks leading to logical operators) anyway.

I shun your BOOL for being just like my nemesis, LOGICAL :) Remember that? :)

Oh, and now who doesn’t answer their email? :)

Answer: You don’t!

No, really!

Pfft – I just checked my kfs and playnet mailboxes and all my emails from Mike Wallis have the little “replied” icon next to them aside from on 3/24/08 which I probably replied to from home (“No need to make it sound like cleaning your room when you were 12 y’know”, that one :)

yeah, dat’s da one, no reply for me to that! ;)

happy holidays and all that good ol’ bah humbug!

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: