funky looking conditional statement i think

Started by
17 comments, last by swiftcoder 10 years, 9 months ago


Bregma is this diagram similar to your explanation of a|=b?

Indeed it is.

Stephen M. Webb
Professional Free Software Developer

Advertisement

Bitwise operators and Bit Masks.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

(To be pedantic, bools don't have a number of bits, but instead undergo integer promotion when placed in the context of bitwise operators, where true is promoted to 1 and false is promoted to 0.)

(except when they don't. like when some asshole vendor has decided to store flags in the other 31 bits of their boolean)

(I will be scarred for the rest of my life)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

(To be pedantic, bools don't have a number of bits, but instead undergo integer promotion when placed in the context of bitwise operators, where true is promoted to 1 and false is promoted to 0.)

(except when they don't. like when some asshole vendor has decided to store flags in the other 31 bits of their boolean)

(I will be scarred for the rest of my life)

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

I became a lot more sympathetic to the idea of library defined boolean types when I first ran across a platform with an 8 byte bool. And, no, this wasn't a platform where everything was 64 bits. sizeof(bool) was 8. gcc on Alpha IIRC.

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

That *was* the integrated bool type.

See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

That's why I don't like it when people can't just use the integrated bool type. It seems like every lib that comes along these days has to have its own proprietary bool.

That *was* the integrated bool type.

See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...


Burn it with fire.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.


See, C++ doesn't really enforce many rules. And if you happen to know that on your platform, the compiler allocates 4 bytes for a bool, then you can *(int)&booleanValue, and proceed to manipulate the bits...

This is actually very dangerous when doing language interop, even when the boolean types have the same size. C/C++ would seem to assume that 0 is false and anything else is true, but it is by no means certain that this is the same for all languages. For instance some other languages consider even = false and odd = true (i.e. they look only at the least significant bit, which imho makes more sense, but whatever). In these situations if you hack your "boolean" variable to equal, say, 42 in C++, this will evaluate to true in C++ but false in that other language. And it's not fun to debug.

It happened to me once. I was wondering why the boolean returned by a call to a C++ library I used from Pascal years ago didn't evaluate to what it should have. Turned out their idea of a boolean was not true/false but more like true/false + handle to some internal structure (because apparently, returning that handle by reference wasn't good enough and they needed to squeeze a copy in the return value "for my convenience"). /sigh

But this all goes with the rest.. people trying to be clever by finding loopholes in the standard and ending up shooting themselves (or, preferably, their users) in the foot.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


This is actually very dangerous when doing language interop, even when the boolean types have the same size. C/C++ would seem to assume that 0 is false and anything else is true, but it is by no means certain that this is the same for all languages.

Bingo.

Proprietary embedded chip, with 4-byte native alignment, and a CMP instruction that only looked at the least-significant bit.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement