regarding << and >> operators

O

onkar

#include<stdio.h>
int main(void){
printf("%d %d\n",32<<1,32<<0);
printf("%d %d\n",32<<-1,32<<-0);
<----------------------------------see here
printf("%d %d\n",32>>1,32>>0);
printf("%d %d\n",32>>-1,32>>-0);
<----------------------------------and here

return 0;
}



$ make
gcc -Wall -g -o test test.c
test.c: In function `main':
test.c:4: warning: left shift count is negative
test.c:6: warning: right shift count is negative
$ ./test
64 32
0 32
16 32
0 32
 
J

jaysome

#include<stdio.h>
int main(void){
printf("%d %d\n",32<<1,32<<0);
printf("%d %d\n",32<<-1,32<<-0);
<----------------------------------see here
printf("%d %d\n",32>>1,32>>0);
printf("%d %d\n",32>>-1,32>>-0);
<----------------------------------and here

return 0;
}



$ make
gcc -Wall -g -o test test.c
test.c: In function `main':
test.c:4: warning: left shift count is negative
test.c:6: warning: right shift count is negative
$ ./test
64 32
0 32
16 32
0 32

Looks like a bug to me and PC-lint agrees with the result:

--- Module: c.c (C)
_
printf("%d %d\n",32<<-1,32<<-0);
c.c(4) : Warning 504: Unusual shift operation (unusually formed right
argument)
c.c(4) : Info 778: Constant expression evaluates to 0 in operation
'<<'
c.c(4) : Info 778: Constant expression evaluates to 0 in operation '-'
c.c(4) : Warning 504: Unusual shift operation (unusually formed right
argument)
_
printf("%d %d\n",32>>-1,32>>-0);
c.c(6) : Warning 504: Unusual shift operation (unusually formed right
argument)
c.c(6) : Info 778: Constant expression evaluates to 0 in operation
'>>'
c.c(6) : Info 778: Constant expression evaluates to 0 in operation '-'
c.c(6) : Warning 504: Unusual shift operation (unusually formed right
argument)

--- Global Wrap-up

Note 900: Successful completion, 8 messages produced
Tool returned code: 0

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top