Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A mistake in 4 simple lines!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Wed Jan 10, 2007 12:39 am   Post subject: 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:

code:
int main(){
        long long x = 1 << 40;
        printf ("%d", x >> 40);
return 0;}


Since there are 8 bits in 1 byte, 8 bytes will give you 64 bits, so 1 << 40 should still be in the range. However, when I tried to compile it, GCC gave me this warning:

testing.c:4: warning: left shift count >= 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.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Wed Jan 10, 2007 12:49 am   Post subject: RE:A mistake in 4 simple lines!

you may have to use a cast in there somewhere, perhaps static_cast<long long>(1) << 40. Otherwise 1 << 40 is being performed as an integer operation, which does indeed result in an overflow.
wtd




PostPosted: Wed Jan 10, 2007 2:37 am   Post subject: RE:A mistake in 4 simple lines!

What about using the constant:

code:
1LL
md




PostPosted: Wed Jan 10, 2007 3:04 am   Post subject: RE:A mistake in 4 simple lines!

That would work too... but as I have never used long longs I wasn't entirely sure what the constant was Razz
HazySmoke)345




PostPosted: Wed Jan 10, 2007 9:44 pm   Post subject: Re: A mistake in 4 simple lines!

Quote:
you may have to use a cast in there somewhere, perhaps static_cast<long long>(1) << 40. Otherwise 1 << 40 is being performed as an integer operation, which does indeed result in an overflow.


Um... I'm not sure what is the "static_cast" part about... Sad, but nevertheless, casting the 1 into the long long data type does work. It doesn't matter if it's (long long)1 or 1LL, so thanks guys, but I have one more question.

Now I've successfully stored this HUGE number into the memory, is it possible to actually print it out on the screen (as in, print out the number 1099511627776, which is 1 << 40). I tried to use "%lld" to represent x, but that doesn't seem to work. In fact, does printf even support this HUGE data type?
md




PostPosted: Wed Jan 10, 2007 10:38 pm   Post subject: RE:A mistake in 4 simple lines!

/me notes this is C help...

Were it C++ I'd say use std::cout, but for C I'm not to sure. I'm positive someone has had the issue before so google might be able to help; I've got no idea however.
bugzpodder




PostPosted: Wed Jan 10, 2007 10:38 pm   Post subject: RE:A mistake in 4 simple lines!

printf does support long longs. Try %ld
Monstrosity_




PostPosted: Thu Jan 11, 2007 9:35 pm   Post subject: Re: A mistake in 4 simple lines!

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:
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.

And why wouldn't you use unsigned long long for a larger number?

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:

code:
int main(){
        long long x = 1 << 40;
        printf ("%d", x >> 40);
return 0;}


Assuming your compiler has support for it, use %lld or %llu conversion specifier.

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:

Since there are 8 bits in 1 byte

Nope, that would be an octet.

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:

1 << 40 should still be in the range. However, when I tried to compile it, GCC gave me this warning:

testing.c:4: warning: left shift count >= 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.

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:

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.

HazySmoke)345 @ Wed Jan 10, 2007 1:39 am wrote:

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.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: