Not getting the desired o/p with ~ operator. Kindly help.

This is a discussion on Not getting the desired o/p with ~ operator. Kindly help. within the C Programming forums, part of the General Programming Boards category; Hi everybody, I was wondering if you could help me with this ~ operator. I just tried the following simple ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Question Not getting the desired o/p with ~ operator. Kindly help.

    Hi everybody, I was wondering if you could help me with this ~ operator.

    I just tried the following simple program in my Turbo C;

    Code:
    int main() {
    
    unsigned short int i = ~0;
    printf("%d", i);
    
    return 0;
    }
    i expected the program to return the maximum value that can be stored i.e: 65535 but the o/p produced was -1. Also, when i tried the same program in codepad.org (online compiler), the o/p was 65535. And in both my PC and the online version the ~ doesn't seem to be producing the expected o/p for other integer types such as "int", "long" etc.

    Can you someone please tell me if i'm doing anything wrong? Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well on your fossilised TurboC, short int and int are the same size, so the MSB of ~0 is also the sign bit - you get -1

    On some other machine, short int is 16 bits, (0x0000 or 0xFFFF) and an int is 32-bits.
    So it can comfortably hold the promoted unsigned value of 65535 and that is what you see.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    @Salem Thanks for the clarification. can you recommend a good compiler to run on my Win7? I'm returning to C programming after many years so trying to catchup

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DevinLgls View Post
    @Salem Thanks for the clarification. can you recommend a good compiler to run on my Win7? I'm returning to C programming after many years so trying to catchup
    Free C / C++ Compilers and Interpreters (thefreecountry.com)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3

    Thumbs up

    Salem & CommonTater. Thanks a lot for your suggestions. this forum rocks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. kindly help
    By Omer in forum C Programming
    Replies: 8
    Last Post: 05-02-2010, 02:03 AM
  2. Kindly help!!!
    By Fatima Rizwan in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2009, 07:17 PM
  3. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  4. Much desired program.
    By MannyCalavera in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 09-23-2005, 05:40 PM
  5. not getting the desired output. WHY???
    By KristTlove in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2003, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21