This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Left shift operations and the long long int datatype


I've been playing around with the '<<' bit-shift operator using the long
long int datatype and came across some funnies with it.

Here's a short sample test program

/*
	Test to demonstrate some strangeness with 
	left shift operations on long long ints
*/

#include <stdio.h>

#define FIND_MAX(type) \
	(type)(~(1 << ((sizeof(type) * 8) - 1)))

int main(void)
{
	signed long long int s64 = FIND_MAX(signed long long int);
	signed long int s32 = FIND_MAX(signed long int);
	signed short int s16 = FIND_MAX(signed short int);
	signed char s8 = FIND_MAX(signed char);

	unsigned long long int u64 = -1;
	unsigned long int u32 = -1;
	unsigned short int u16 = -1;
	unsigned char u8 = -1;

	printf("u64 = %llu, u32 = %lu, u16 = %hu, u8 = %u\n", 
		u64, u32, u16, u8);

	printf("s64 = %lld, s32 = %ld, s16 = %hd, u8 = %d\n",
		s64, s32, s16, s8);

	return 0;
}

The output is:

u64 = 18446744073709551615, u32 = 4294967295, u16 = 65535, u8 = 255
s64 = -1, s32 = 2147483647, s16 = 32767, u8 = 127

You'll see that the s32, s16 and s8 values are correctly left-shifted to
the appropriate number of bits using my little macro. The unsigned types
are all correctly handled but then I'm not using the shift operator for
them. 

However, here's where it gets strange, anything beyond the 31st bit
doesn't seem to work properly. 

When you compile the test program it comes up with a warning:
longlong.c: In function `main':
longlong.c:15: warning: left shift count >= width of type

Now, I am thinking that the gcc compiler may not be realising that the
long long int data-type is 64 bits in length, but it is refusing to go
beyond the 32nd bit, even though it's a 64 bit value. 

Surely it could emit extra code to do the shift beyond the 32nd bit on
32bit architectures? It'd make sense to do that if we've got long long
types implemented? 

Or am I being a dummy or something? 

I'm using gcc-2.95.1 on a Linux powered dual Pentium Pro. 

Cheers, 
Alex 
-- 

Legalise cannabis today!

http://www.tahallah.demon.co.uk - updated!


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]