473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Simple questions on shift ops and promotions

Just to get my head straight on this...

Firstly, am I right in thinking that right-shifting a signed integer has an
undefined result (i.e. could be implemented as a logical or arithmetic
shift)?

If so, am I also right in assuming that if I right-shift a signed integer by
an unsigned integer, the signed value will be promoted to unsigned and the
shift will be performed as a logical right shift?

Finally, if I specify an int literal, am I right in thinking that it's
treated the same way as any other signed integer, and will be promoted to
unsigned in the same way?

thanks in advance,
glen.

Jul 22 '05 #1
4 2598
Glen Able wrote:
Just to get my head straight on this...

Firstly, am I right in thinking that right-shifting a signed integer
has an undefined result (i.e. could be implemented as a logical or
arithmetic shift)?
"If E1 has a signed type and a negative value, the resulting value is
implementation-defined."

This says: nope.
If so, am I also right in assuming that if I right-shift a signed
integer by an unsigned integer, the signed value will be promoted to
unsigned and the shift will be performed as a logical right shift?
As above: it is implementation defined. The docs of your compiler should
tell it.
Finally, if I specify an int literal, am I right in thinking that it's
treated the same way as any other signed integer, and will be
promoted to unsigned in the same way?


The above are exrepts from the standard, talking about right shift operator.
I believe that they answer your questions. As I see no promotions are done
from signed to unsigned. IIRC that is not done just anywhere, only if the
required type is unsigned - which seems not to be true in this case. I
might be wrong, but I think you need that u to make your literal unsigned.

--
Attila aka WW
Jul 22 '05 #2
On Tue, 27 Jan 2004 15:31:38 +0200, "Attila Feher"
<at**********@lmf.ericsson.se> wrote in comp.lang.c++:
Glen Able wrote:
Just to get my head straight on this...

Firstly, am I right in thinking that right-shifting a signed integer
has an undefined result (i.e. could be implemented as a logical or
arithmetic shift)?


"If E1 has a signed type and a negative value, the resulting value is
implementation-defined."

This says: nope.
If so, am I also right in assuming that if I right-shift a signed
integer by an unsigned integer, the signed value will be promoted to
unsigned and the shift will be performed as a logical right shift?


As above: it is implementation defined. The docs of your compiler should
tell it.


No, you are incorrect here. The type of the result is the type of the
promoted left operand. The type of the right operand (the shift
count) does not enter into the promotion of the left operand (the
value shifted).
Finally, if I specify an int literal, am I right in thinking that it's
treated the same way as any other signed integer, and will be
promoted to unsigned in the same way?


The above are exrepts from the standard, talking about right shift operator.
I believe that they answer your questions. As I see no promotions are done
from signed to unsigned. IIRC that is not done just anywhere, only if the
required type is unsigned - which seems not to be true in this case. I
might be wrong, but I think you need that u to make your literal unsigned.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
On Tue, 27 Jan 2004 13:21:59 -0000, "Glen Able"
<re***********************@hoxtmxail.com> wrote in comp.lang.c++:
Just to get my head straight on this...

Firstly, am I right in thinking that right-shifting a signed integer has an
undefined result (i.e. could be implemented as a logical or arithmetic
shift)?
The result of right shifting a signed integer type with a negative
value is implementation-defined, as Attila, said, unless it generates
an invalid value. If it does not generate an invalid value, it is
implementation-defined whether the sign bit is retained as a 1 or is
set to 0. If it does generate an invalid value, then you generate
undefined behavior.
If so, am I also right in assuming that if I right-shift a signed integer by
an unsigned integer, the signed value will be promoted to unsigned and the
shift will be performed as a logical right shift?
No, any promotion of the left hand operand (value shifted) is
completely unaffected by the type of the right hand operand (shift
count). Specifically shifting a value of any signed integer type by
an unsigned shift count does NOT convert the value shifted to
unsigned. If the signed type has lesser rank than int (signed char,
signed short, bool), it will be promoted to signed int, not unsigned
int.
Finally, if I specify an int literal, am I right in thinking that it's
treated the same way as any other signed integer, and will be promoted to
unsigned in the same way?
You are right in thinking that it will be treated the same way as any
other signed integer, but that specifically means that it will NOT be
converted to unsigned just because the second operand (shift count) is
unsigned. If you want to shift an unsigned integer literal, append
'U'.
thanks in advance,
glen.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4
Jack Klein wrote:
On Tue, 27 Jan 2004 15:31:38 +0200, "Attila Feher"
<at**********@lmf.ericsson.se> wrote in comp.lang.c++:
Glen Able wrote:
Just to get my head straight on this...

Firstly, am I right in thinking that right-shifting a signed integer
has an undefined result (i.e. could be implemented as a logical or
arithmetic shift)?


"If E1 has a signed type and a negative value, the resulting value is
implementation-defined."

This says: nope.
If so, am I also right in assuming that if I right-shift a signed
integer by an unsigned integer, the signed value will be promoted to
unsigned and the shift will be performed as a logical right shift?


As above: it is implementation defined. The docs of your compiler
should tell it.


No, you are incorrect here. The type of the result is the type of the
promoted left operand. The type of the right operand (the shift
count) does not enter into the promotion of the left operand (the
value shifted).


Read 5.8/1

"The operands shall be of integral or enumeration type and integral
promotions are performed. The type of the result is that of the promoted
left operand. The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left operand."

Read: "promoted left operand".

1.) The left operand is promoted with the usual integer promotions.

2.) AFAIK that cannot result in it being promoted to unsigned

3.) I did not tell the type of the shift-count will trigger promotion

4.) I did note that if the signed value is negative one should read the docs

I have however made the mistake that by "signed integer" I have assumed a
negative value there.

--
Attila aka WW
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: bml | last post by:
Q1: int i = 0; char *p = i; Is this assign 0 to p or assigning the value pointed by p to 0? Is this a problem since p has not been initialized, it could point to any place in memory and assign...
12
by: DeepaK K C | last post by:
main() { int a =100; a = a>>32; printf(" %d", a); } it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
2
by: fizzleBomb | last post by:
After running this: unsigned long long x = (unsigned long long)-1; int y = 64; x = x >y; the value of x is 0xffffffffffffffff though I'd have expected 0. Strangely, shifting by 63 produces 1...
7
by: L.G | last post by:
I met one problem of shift a number with equal or more than 32 bits. When running this : uint32_t num = 0x45F5F; int shift = 33; uint32_t res = num << shift; printf( "\\exp: %08XH << %d =...
14
by: Thurston Manson | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
5
hyperpau
by: hyperpau | last post by:
I just have some stupid questions but really want to know to fully understand vba more. What are the arguments for in some of the events? For example, the click event has the arguments...
1
joedeene
by: joedeene | last post by:
Hello all, I am using Macromedia Flash Professional 8 in this example of a simple way to use motion tweens. Ok here goes... Directions: A. Preparation 1.) Start the Flash program. Once...
35
by: Lew Pitcher | last post by:
On November 14, 2008 15:00, in comp.lang.c, Nomen Nescio (nobody@dizum.com) wrote: Overkill Try
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.