CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C# int query

  1. #1
    Join Date
    Mar 2015
    Posts
    3

    C# int query

    Hi guys, I'm going through Microsoft's Developer Step By Step Visual C# 2013 and in chapter 16 I have come across a bit of explanation that i'm not quite understanding, I've taken a photo:

    https://dl.dropboxusercontent.com/u/...307_150640.jpg

    I was fine up til the last sentence in the Note box "which is actually the int representation of -205 in C#"

    I understand that in binary, 204 is:

    00000000000000000000000011001100

    I understand that ~204 (NOT 204) must be:

    11111111111111111111111100110011 - just flip the 1s and 0s

    However, I cannot understand how:

    11111111111111111111111100110011 = the int representation of -205 in C#.

    I would have said that 11111111111111111111111100110011 (using a decimal calculator) was 4,294,967,091. If it actually is -205, then what is the binary for 4,294,967,091 in C#?

    I must be missing something fundamental here, can someone explain?

    thanks!

  2. #2
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# int query

    remember that a int and a uint (unsigned) are not the same

    the maximum positive value in binary for a int is 0111-1111-1111-1111-1111-1111-1111-1111 = 2147483647
    but that's Not the maximum value for a unsigned integer

    so ask what is that final bit for in a int ?
    yep that's the sign flag it counts as -1

    hence -1
    1111-1111-1111-1111-1111-1111-1111-1111
    Note there is no 0 or significant inverse digit however the sign bit here denotes it is -1 in decimal
    i.e. for the decimal value of zero we can say it is considered positive
    essentially the sign bit is not on for a decimal value of zero

    however as a unsigned integer the above binary is the maximum value of a uint equal to 4294967295

    to represent -2 we flip on the sign bit and mark the 1's place inversely significant
    1111-1111-1111-1111-1111-1111-1111-1110

    for -3 we have with the sign bit
    1111-1111-1111-1111-1111-1111-1111-1101
    the inverse significant digit is in the twos place
    with the additional -1 sign bit implicitly added

    and for -4 you can guess
    1111-1111-1111-1111-1111-1111-1111-1100
    e.g.
    if this were positive it would be +1 +2 = 3
    but since its in the inverse you count like so
    you go (-1)+-1+-2 = -4


    I don't normally think of negatives as inverse
    i think about if the sign bit is on or not
    In fact i had forgotten how it worked i had to think about it.

    you might experiment by setting individual bit positions on or off
    Code:
            public static int SetIntsBit(int theinteger, int bitposition, bool true_or_false)
            {
                int bitvalue = 1 << (bitposition);
                if ((theinteger & bitvalue) > 0)
                {
                    if (true_or_false == false)
                    {
                        theinteger = theinteger ^ bitvalue;
                    }
                }
                else
                {
                    if (true_or_false == true)
                    {
                        theinteger = theinteger | bitvalue;
                    }
                }
                return theinteger;
            }
    Last edited by willmotil; March 7th, 2015 at 04:23 PM.

  3. #3
    Join Date
    Mar 2015
    Posts
    3

    Re: C# int query

    Ah thanks for the explanation, i'd covered int types but nothing that went into the binary representations of the different types etc.

    So I guess an unsigned int gets that extra range because it stops using the leading bit as a potential indicator of a negative number, so it gets to actually use it. But with a signed int the leading bit is given up for this use, and only 31 bits can actually be used for the number.

    It does seem to get a bit unintuitive though looking at the binary versions of negative numbers, I can see why you had to think about it. I hope I wont have much dealings with them in the future!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C# int query

    Actually there is no "sign bit" with 2s complement (which is that is almost universally used). With a sign bit notation (still used in a few places) the pattern would be 10000....0001 (first bit is the sign, then the remaining bits are the absolute value. To understand 2s complement, think about this (I am going to use a much shorted word length)

    If you have 11111 and Add 0001, but are limited to 5 bits, what do you get? 00000 !!... ZERO... But what number can you add 1 to and end up with zero? -1..
    If you have 11110 and Add 0010, but are limited to 5 bits, what do you get? 00000 !!... ZERO... But what number can you add 1 to and end up with zero? -2..

    And so on...

    Make sense??
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Mar 2015
    Posts
    3

    Re: C# int query

    i'm struggling, but i'm watching some youtube videos that seem to explain it well. hopefully it will sink in !

    From what I understand, aren't those examples overflow situations which simply indicates that the calculation is unsupported ?

    What I also wonder is, given no other information than a binary number like 1101, exactly where is the indicator of whether the number is signed or unsigned ? If there never is any such indicator, isn't that somewhat problematic ?
    Last edited by pigmonkeys21; March 12th, 2015 at 09:02 AM.

  6. #6
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# int query

    sorry he's right, i probably said that poorly, though it still denotes the sign but it is a value as well
    to say no (positive signed integer) number in either in will have that first bit set true

    see the chart on the page in the link below, it will probably make what i said above more clear

    this link has two charts that shows all the values of a signed nibble for both types
    0 to F hex, or -8 to 7 decimal, or 0000 to 1111 binary
    http://www.exploringbinary.com/twos-...ent-converter/

    now as far as overflow underflow im not sure you would have to look that up, i would guess
    that probably is handled in machine code at the register level on the cpu with specific rules
    such things only may occur when a operation is performed such as addition subtraction ect...

    there are quite a few sites you can simply google as well that may click a bit better
    http://academic.evergreen.edu/projec...am/2s_comp.htm
    Last edited by willmotil; March 12th, 2015 at 03:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured