Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Doubt in C programming

Status
Not open for further replies.

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
#include<stdio.h>
main()
{
printf("%X",-1>>1);
}


i am getting the output is FFFFFFFF in linux..but actual output is 7FFFFFFF...

Please give me explanation why the output is FFFFFFFF
 

-1 is internally represented as all 1's. When right shifted the msb bit has been filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value as7FFFFFFF
 

-1 is 0xFFFFFFFF so if you shift it right by one you'll get 0x7FFFFFFF. I guess you're aware that you shift the result right by one
 

but when iam compiling this using GCC Compiler i am ggetting the output as ffffffff
 

May I suggest you define the constant properly as 0xFFFFFFFF instead of -1 and try again

-1 is not totally defined - it could be 0xFF, or 0xFFFF or 0xFFFFFFFF. If you leave some ambiguity for the compiler it could do crazy things for you
 

I agree with all the previous posts,
-1 is internally represented as all 1's, when you shift right one then the MSB will become a 0 so the four MSB bits will be 0111 which is represented by 0x7,
depending on the bit width of the number this will be followed by "F"s 0x7FFF....

Alex
 

#include<stdio.h>
main()
{
printf("%X",-1>>1);
}


i am getting the output is FFFFFFFF in linux..but actual output is 7FFFFFFF...

Please give me explanation why the output is FFFFFFFF
the C and C++ standards state for the >> operator
">> the bit pattern is shifted right, the value shifted in from the left may be 0's (logical shift) or a copy of the sign bit (arithmetic shift); it is implementation dependent."
Hence, if the system uses arithmetic shift you get FFFFFFFF, if logical shift 7FFFFFFF.
These system dependent 'features' of C and C++ can cause lots of problems when porting code and watse days of your time if you are not careful with the orginal implementation.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top