If an integer is positive or negative

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepankarrawat
    New Member
    • Apr 2010
    • 1

    #1

    If an integer is positive or negative

    How will i know in C programming if an integer is negative or positive?
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    If it is greater than zero it will be positive and if it is less than zero it will be negative.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by deepankarrawat
      no details
      Please provide some details. What exactly is your question?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        ... you ask the program?

        ... the first bit of the integer will be set if it is negative. If it is not set, the number is non-negative. If the integer is zero, none of the bits will be set.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          I think mine is the best answer. I'm choosing it, because I'm awesome!

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by jkmyoung
            ... the first bit of the integer will be set if it is negative. If it is not set, the number is non-negative ...
            This is only true if signed integers are encoded using two's-complement. Although two's-complement is ubiquitous, it is not mandated by the C Standard. This approach is not portable.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Oooh don throws a right hook at jkm!! Will he parry or duck?

              He may be down for the count folks!

              (can you tell I'm having fun?)

              Comment

              Working...