public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53369] New: Integral promotion with ~ operator - Different behavior for signed and unsigned char.
@ 2012-05-16  0:04 rahulnitk2004 at gmail dot com
  2012-05-16  0:05 ` [Bug c/53369] " rahulnitk2004 at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: rahulnitk2004 at gmail dot com @ 2012-05-16  0:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53369

             Bug #: 53369
           Summary: Integral promotion with ~ operator - Different
                    behavior for signed and unsigned char.
    Classification: Unclassified
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: rahulnitk2004@gmail.com


Created attachment 27413
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27413
The preprocessed file

I face a problem when i use bitwise ~ operator on signed char. With unsigned
char, it works as per the K&R ANSI C specification.

Here is the code snippet.

    signed char a = 1 << 7;
    unsigned char b = 1 << 7;

    printf("%hd  %hu\n", ~a, ~b);

Output for the program is shown as: 127  65407.

As per the integral promotion concept, a char variable whether signed or
unsigned will be converted to int before performing an operation on it.
So before performing (~a), a will be converted to int and will have 32 bits
(In my machine int is 32 bit). So ~a should result in 0XFFFFFF7F. So when i
print ~a it should result in 0XFF7F as format specifier is "%hd". Instead it is
only 127(0X7F). 
Similarly for unsigned char b, ~b should result in 0XFFFFFF7F and output should
be 65407(0XFF7F) as i used "%hu" as format specifier. That is printed
correctly. 

I am using GCC version 4.5.2 on Ubuntu.

Any suggestions?

Thanks
Rahul
---------------------------------------------------------------------------

Code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    signed char a = 1 << 7;
    unsigned char b = 1 << 7;

    printf("%hd  %hu\n", ~a, ~b);

    return(EXIT_SUCCESS);
}

Compilation command used: gcc -Wall test.c (No warnings).

Output: 127  65407

------------------------------------------------------------------------------

Other Deatils:

------------------------------------------------------------------------------
rahul@ubuntu:~/sample$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.5 --enable-shared --enable-multiarch
--with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu
--without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/x86_64-linux-gnu
--enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default
--with-plugin-ld=ld.gold --enable-objc-gc --disable-werror --with-arch-32=i686
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
------------------------------------------------------------------------------

rahul@ubuntu:~/sample$ gcc -v -save-temps -Wall test.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.5 --enable-shared --enable-multiarch
--with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu
--without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/x86_64-linux-gnu
--enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default
--with-plugin-ld=ld.gold --enable-objc-gc --disable-werror --with-arch-32=i686
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-mtune=generic' '-march=x86-64'
 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1 -E -quiet -v test.c
-D_FORTIFY_SOURCE=2 -mtune=generic -march=x86-64 -Wall -fpch-preprocess
-fstack-protector -o test.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/include
 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-mtune=generic' '-march=x86-64'
 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/cc1 -fpreprocessed test.i
-quiet -dumpbase test.c -mtune=generic -march=x86-64 -auxbase test -Wall
-version -fstack-protector -o test.s
GNU C (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8,
MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8,
MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9755ab75799195519479ef699703b13b
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-mtune=generic' '-march=x86-64'
 as -V -Qy --64 -o test.o test.s
GNU assembler version 2.21.0 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.21.0.20110327
COMPILER_PATH=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/:/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/:/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/:/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/:/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/:/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../:/lib/:/usr/lib/:/usr/lib/x86_64-linux-gnu/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-mtune=generic' '-march=x86-64'
 /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/collect2 --build-id
--eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -z relro
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crti.o
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/crtbegin.o
-L/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2
-L/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../..
-L/usr/lib/x86_64-linux-gnu test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc
-lgcc --as-needed -lgcc_s --no-as-needed
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/crtend.o
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crtn.o
-------------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-05-16  9:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-16  0:04 [Bug c/53369] New: Integral promotion with ~ operator - Different behavior for signed and unsigned char rahulnitk2004 at gmail dot com
2012-05-16  0:05 ` [Bug c/53369] " rahulnitk2004 at gmail dot com
2012-05-16  0:12 ` rahulnitk2004 at gmail dot com
2012-05-16  1:39 ` pinskia at gcc dot gnu.org
2012-05-16  9:12 ` rahulnitk2004 at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).