Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. output confusion

output confusion

Scheduled Pinned Locked Moved C#
csharp
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Unknown Ajanabi
    wrote on last edited by
    #1

    Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.

    no knowledge in .net

    E D realJSOPR 3 Replies Last reply
    0
    • U Unknown Ajanabi

      Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.

      no knowledge in .net

      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      sbyte (signed byte) has a range from -128 to 127. Because you used unchecked no overflow is detected. So, 255 is signed -1 :) . Cheers

      I don't like my signature at all

      1 Reply Last reply
      0
      • U Unknown Ajanabi

        Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.

        no knowledge in .net

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        The sign is represented by the MSb (most significant bit).

        Binary Hex byte sbyte
        00000000 0x00 0 0
        ...
        01111111 0x7F 127 127
        10000000 0x80 128 -128
        ...
        11111111 0xFF 255 - 1

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        modified on Saturday, June 5, 2010 9:09 AM

        1 Reply Last reply
        0
        • U Unknown Ajanabi

          Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.

          no knowledge in .net

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Because a sbyte is a "short byte", meaning it's max value is half of what a normal byte would hold. In your case, your value is causing an interger overflow condition, and the value becomes -1.

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          D 1 Reply Last reply
          0
          • realJSOPR realJSOP

            Because a sbyte is a "short byte", meaning it's max value is half of what a normal byte would hold. In your case, your value is causing an interger overflow condition, and the value becomes -1.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            I thought the 's' stood for signed rather than short as it's still 8 bits so no shorter. Either way the answer to the OP is the same and correct :thumbsup:

            Dave

            If this helped, please vote & accept answer!

            Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            L 1 Reply Last reply
            0
            • D DaveyM69

              I thought the 's' stood for signed rather than short as it's still 8 bits so no shorter. Either way the answer to the OP is the same and correct :thumbsup:

              Dave

              If this helped, please vote & accept answer!

              Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You're right, however if we are going to be picky, maybe you'd better fix the mistake in

              10000000 0x80 128 -127

              too? :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              D E 2 Replies Last reply
              0
              • L Luc Pattyn

                You're right, however if we are going to be picky, maybe you'd better fix the mistake in

                10000000 0x80 128 -127

                too? :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                ;P :-O :laugh: Fixed!

                Dave

                If this helped, please vote & accept answer!

                Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                L 2 Replies Last reply
                0
                • L Luc Pattyn

                  You're right, however if we are going to be picky, maybe you'd better fix the mistake in

                  10000000 0x80 128 -127

                  too? :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  E Offline
                  E Offline
                  Estys
                  wrote on last edited by
                  #8

                  I saw that too. That led me to revisit the two's-complement calculation and I found this[^]. "the universe is twos-complement" Cheers

                  I don't like my signature at all

                  1 Reply Last reply
                  0
                  • D DaveyM69

                    ;P :-O :laugh: Fixed!

                    Dave

                    If this helped, please vote & accept answer!

                    Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Long ago Data General used to build 36-bit machines, where an integer by default was 36 bits; they also had instructions that treated registers holding five 7-bit "bytes" plus a single flag. They used that for representing text, based on ASCII characters, which requires 7 bits. That could rightfully be called short bytes. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    D 1 Reply Last reply
                    0
                    • D DaveyM69

                      ;P :-O :laugh: Fixed!

                      Dave

                      If this helped, please vote & accept answer!

                      Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      the 100...00 bit pattern happens to be the only one you can't represent the absolute value of; and lots of run-time libraries don't treat it well, e.g. an sprintf-like function turning a signed integer to its decimal string representation wants to do:

                      if (value) {
                      emit('-');
                      value = -value; // <<<<<<<<<
                      }
                      // now process positive number

                      which isn't correct, as value may still be negative yielding unexpected quotient/remainder values later on. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Long ago Data General used to build 36-bit machines, where an integer by default was 36 bits; they also had instructions that treated registers holding five 7-bit "bytes" plus a single flag. They used that for representing text, based on ASCII characters, which requires 7 bits. That could rightfully be called short bytes. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #11

                        How to handle bit lengths for types values that don't require all of them or require more is still an issue in some situations. In my MIDI exploits I'm working with 16bit PICs at the moment. The choice between the complexity and therefore instruction cycle overhead of storing 2 bytes in one word (made more complex as packets can be variable lengths so won't always be word aligned) and the waste of scarce resources by ignoring 8 bits for every byte I need to buffer is one I'm still debating. MS[^] use one 32bit int (all packets, except SysEx which is easy to identify, are 1 to 3 bytes) and 'waste' between 8 and 24 bits each time. Fine on a PC but not efficient useage of RAM on a µC with only 2KB RAM when requiring 4 individual buffers.

                        Dave

                        If this helped, please vote & accept answer!

                        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        L 1 Reply Last reply
                        0
                        • D DaveyM69

                          How to handle bit lengths for types values that don't require all of them or require more is still an issue in some situations. In my MIDI exploits I'm working with 16bit PICs at the moment. The choice between the complexity and therefore instruction cycle overhead of storing 2 bytes in one word (made more complex as packets can be variable lengths so won't always be word aligned) and the waste of scarce resources by ignoring 8 bits for every byte I need to buffer is one I'm still debating. MS[^] use one 32bit int (all packets, except SysEx which is easy to identify, are 1 to 3 bytes) and 'waste' between 8 and 24 bits each time. Fine on a PC but not efficient useage of RAM on a µC with only 2KB RAM when requiring 4 individual buffers.

                          Dave

                          If this helped, please vote & accept answer!

                          Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          I tend to first take care of whatever resource is most scarce. On miniature systems that would be memory most of the time. So decide on a clever data scheme, then come up with efficient accessor functions, even if that takes some ugly code and hacks. Maybe create a stream-like object? :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          I only read formatted code with indentation, so please use PRE tags for code snippets.


                          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups