Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

<< left shift operator

1 view
Skip to first unread message

Brandy Bogard

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
When the left shift operator is used in an expresion like:

char Temp = 12;
long Result;

Result = (Temp << 24);

Does the compiler convert the left shift operation to be a full 32 bits, or
does it get truncated?


Paul Lutus

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
The answer depends on the platform and the size of the data types. On my
compiler, the variable temp is promoted to long during the shift, so the
result is meaningful. This may be standard behavior -- I don't know.

But you need to realize that not all platforms provide 32 bits for a
particular data type. To discover the size of your platform's data types,
use the "sizeof" keyword. Example:

printf("size of long = %d\n",sizeof(long)); /* expressed in bytes */

Paul Lutus


Brandy Bogard wrote in message <76e4fq$d7s$1...@hops.adnc.com>...

N8TM

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
>Subject: << left shift operator
>From: "Brandy Bogard" <bbo...@windowslink.com>
>

>char Temp = 12;
>long Result;
>
>Result = (Temp << 24);
>
>Does the compiler convert the left shift operation to be a full 32 bits, or
>does it get truncated?

Ideally, there will be a warning if the result is all zero. But why not write

Result = ((long int)Temp << 24);

if that's what you mean?
Tim Prince
tpr...@computer.org

Jack Klein

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to

<Jack>

Shifting a value by a number greater than the number of bits in the
data type produces undefined behavior. No diagnostic is required.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.


Jack Klein

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
On Wed, 30 Dec 1998 13:04:11 -0800, "Brandy Bogard"
<bbo...@windowslink.com> wrote:

> When the left shift operator is used in an expresion like:
>

> char Temp = 12;
> long Result;
>
> Result = (Temp << 24);
>
> Does the compiler convert the left shift operation to be a full 32 bits, or
> does it get truncated?

<Jack>

The char will be converted to int before shifting. If int is long
enough to be left shifted by 24 bits, that is for all practical
purposes int is 32 bits in your implementation, then the code will do
exactly what you want. If int is only 16 bits on your implementation,
left shifting it by 24 bits results in undefined behavior and anything
could happen.

The best way to write this portably is to use a cast:

Result = (long)Temp << 24;

sc...@softbase.com

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
Brandy Bogard (bbo...@windowslink.com) wrote:
> When the left shift operator is used in an expresion like:

> char Temp = 12;
> long Result;

> Result = (Temp << 24);

> Does the compiler convert the left shift operation to be a full 32 bits, or
> does it get truncated?

The easiest way to answer this question is to tell the compiler to
generate an ASM listing, or step through this code in the debugger. If
I had to guess, I would say Temp is automatically promoted to whatever
"24" is -- an integer, but what is an integer? No one knows, since its
size is system dependent. Most of the time, a long is an int, so
probably nothing would be lost in this operation. Were an int a short,
who knows what would happen? The moral of this story is not to create
code that relies on language-lawyer knowledge of the C language. Even
if you find out the answer, the next person who has to modify the code
might not know it.

You're also shifting a signed number, which is is probably not a good
idea.

Scott

Lawrence Kirby

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
In article <76e81m$f98$1...@remarQ.com> nos...@nosite.com "Paul Lutus" writes:

>The answer depends on the platform and the size of the data types. On my
>compiler, the variable temp is promoted to long during the shift, so the
>result is meaningful. This may be standard behavior -- I don't know.

No, your compiler is almost certainly promoting it to int, as the standard
requires (except for obscure implementations where it promoted to
unsigned int). The integral promotions are applied to the left hand
operand of shift operators which will promote char and short width operands
to an int width type. If your platform happens to implement int and long with
the same representation then it will act then a left shift of an int and a
long operand will act the same. However the promotion is still to int rather
than long and that is the type of the result. So if you pass the result
directly to printf you would need to convert it with, for example,
%d rather than %ld.

>But you need to realize that not all platforms provide 32 bits for a
>particular data type. To discover the size of your platform's data types,
>use the "sizeof" keyword. Example:
>
>printf("size of long = %d\n",sizeof(long)); /* expressed in bytes */

sizeof produces a result of type size_t which is an unsigned integer type.
Therefor %d cannot be a correct conversion specifier for it. If you know
the result will fit in an int you can use

printf("size of long = %d\n",(int)sizeof(long)); /* expressed in bytes */

otherwise use

printf("size of long = %lu\n",(unsigned long)sizeof(long)); /* expressed in bytes */

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Lawrence Kirby

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
In article <368b9...@news.new-era.net> sc...@softbase.com writes:

...

>The easiest way to answer this question is to tell the compiler to
>generate an ASM listing, or step through this code in the debugger. If
>I had to guess, I would say Temp is automatically promoted to whatever
>"24" is -- an integer, but what is an integer?

The left and right operands of the shift operators are promoted independently
so in

char value = 1;
long result;

result = value << 24L;

value is still promoted to int (or rarely unsigned int) and that is the
type of the result of the shift operation.

>No one knows, since its
>size is system dependent. Most of the time, a long is an int, so
>probably nothing would be lost in this operation. Were an int a short,

short, int and long are *always* distinct types. However it is possible
for them to share the same representation.

>who knows what would happen? The moral of this story is not to create
>code that relies on language-lawyer knowledge of the C language. Even
>if you find out the answer, the next person who has to modify the code
>might not know it.

Right.

Brandy Bogard

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
Scott, I don't think think the sign of the number actually matters when
doing a shift. Is that compiler dependent also?

Lawrence Kirby

unread,
Jan 2, 1999, 3:00:00 AM1/2/99
to
In article <76gfr0$mlg$1...@hops.adnc.com>
bbo...@windowslink.com "Brandy Bogard" writes:

>Scott, I don't think think the sign of the number actually matters when
>doing a shift. Is that compiler dependent also?

Partially. A left shift of signed integer types is well defined at the
bit level. However that value of the result is implementation specific
since the representation of negative numbers is implementation-defined.
If you right shift a negative number the result is always implementation
specific. For example the implementation could shift zero bits in at the
left or it could duplicate the sign bit.

0 new messages