
-----------------------------------
HazySmoke)345
Wed Jan 10, 2007 12:39 am

A mistake in 4 simple lines!
-----------------------------------
I recall that, a year ago, I've made two topics in the VB boards called "'A mistake in 5 simple lines" and "A mistake in 3 simple lines". Therefore, we shall continue with the successfulness and create a topic called "A mistake in 4 simple lines". (I DO have a serious question, mind you)

I was trying to create a solver for a puzzle game a few days ago, and my algorithm required the use of a HUGE number, so HUGE that not even the "unsigned long" data type would suffice. After asking some friends that I have, I found that there's this data type called "long long" which creates an 8-byte integer. I tested it by using the sizeof operator and it returned 8, so I thought it worked and started to play around with it by assigning huge numbers to it:

int main(){
	long long x = 1 > 40);
return 0;}

Since there are 8 bits in 1 byte, 8 bytes will give you 64 bits, so 1 = width of type

And with that, my heart sank. I ran the program anyway and it did not work, the program printed a big fat "0" on the screen, while it should have been "1".

So, what's wrong with this code? Is it possible that my computer does not support such a HUGE data type? Or are there some tricks behind this?

P.S. Some suggested to me that I should consider using the floating point data types, but since they do not support bitwise operations, they're useless to me.

-----------------------------------
md
Wed Jan 10, 2007 12:49 am

RE:A mistake in 4 simple lines!
-----------------------------------
you may have to use a cast in there somewhere, perhaps static_cast(1)  40);
return 0;}

Assuming your compiler has support for it, use %lld or %llu conversion specifier.


Since there are 8 bits in 1 byte

Nope, that would be an octet. 


1 = width of type

Yes, 1 is an integer, and most likely 32bits on your implementation. You can do away with this undefined behaviour cast 1 to long with 1LL or use an unsigned type.


And with that, my heart sank. I ran the program anyway and it did not work, the program printed a big fat "0" on the screen, while it should have been "1".

Undefined behaviour, you have no idea what its going to do / output.


P.S. Some suggested to me that I should consider using the floating point data types, but since they do not support bitwise operations, they're useless to me.
Fair enough, you can use long long if you like. Just keep in mind this is restricted to c99 compilers and c89 compilers that support long long as an extension.
