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.

Same program but different output?

Status
Not open for further replies.

bhadmanathan

Junior Member level 1
Joined
May 7, 2016
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
187
Same program but different output ?

Hi ,
int main()
{
char a=1;
printf("%d\n",a<<32);
printf("%d\n",1<<32);
return 0;
}

OUTPUT:
1
0

I am using 32 bit compiler. When i shift beyond the width (0:31) , what is actually happening.

Even though both the printf statements are meant to same, i am getting different output ?


Thanks in advance
 

Re: Same program but different output ?

In the printf you are asking it to print a decimal number but you are supplying it with a char.

What is the expected number if you shift char 1 32 bits to the left?

In the second printf, 1 is taken as a number and not a char. That does make a difference.
 

Re: Same program but different output ?

char is a signed 8 bit number, the second (constant) expression can be expected to be evaluated as int according to the default compiler format (I presume int32).

According to C standard, the overflow behavior of signed numbers is undefined, so any result would be allowed in both cases, not necessarily the same.

The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
 

Re: Same program but different output ?

int main()
{
char a=1;
unsigned char b=1;
int c=1;
unsigned int d=1;
printf("%d\n",a<<32);
printf("%d\n",b<<32);
printf("%d\n",c<<32);
printf("%d\n",d<<32);

printf("%d\n",1<<31);
return 0;
}

O/P;
1
1
1
1
0

Here i am using 32 bit processor, so it can process with 32 bits at a time.When i shift numbers from any variable then it gets shifted in rotational order i.e

a<<30 - fine
a<<31 - fine
a<<32 - gets shifted 0 times
a<<33 - gets shifted 1 time
a<<34 -gets shifted 2 times
......etc

But i shift direct constant beyond the limit ,the answer is always zero.
 

Re: Same program but different output ?

The compiler doesn't seem to fully comply with the C standard. At least for shift of unsigned numbers beyond it's range, the result has to be zero.

Shift left "<<" is not rotate left.

What's the compiler under test?
 

Re: Same program but different output ?

I used
Geeksforgeeks IDE (online)
Dev cpp

In both the compilers i got the same result....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top