right shifting an int16_t value

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

For

int16_t a = -32768 // 8000h

What do we get with

a = a >> 1;

?
Do we get '-16384' (= C000h) or '+16384' (= 4000h)? I just can't find such description in the manual.

Thank you.
Senmeis

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Silly question but exactly how long would it take to write a program to check this yourself?

Last Edited: Fri. Aug 15, 2008 - 10:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

-16384

09:47..09:49 - I make that two minutes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

as clawson said, just try it out. But don't be surprised if you get different results on a different compiler or architecture. The C99 Standard (or the last public draft of it) says in paragraph 6.5.7-5:

C99 wrote:
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

Sorry Senmeis, no definitive answer neither in this case ;-)

Thomas

pycrc -- a free CRC calculator and C source code generator