|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Right Shifting
Hai All,
Here is a some code i executed , its giving some what strange values , right shift means dividing the value by 2 . On this basis i have excuted the code. The code follows #include <stdio.h> int main() { char ch = -0x7f ; int i = ch; printf("%d %d %d %d\n",ch,ch>>1,-127/2,i/2); } Out put is : -127 -64 -63 -63 Here -127/2 and i/2 are giving -63 as a result But ch>>1 is giving -64 as a result . How it is possible ? Please explain me. Suri |
|
#2
|
||||
|
||||
|
-0x7f is 1111111110000001 in binary (assuming a 16 bit variable). Right shifting this by one bit gives us 1111111110000000 which translates to 64 in decimal. Now -127/2 is -63.5 actually. However, when C performs integer divisions (i.e. both numerator and denominator are integers), it always truncates the value. Hence 63.5 will be truncated to 63.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Right Shifting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|