locked
Should we use unsigned int to save space RRS feed

  • Question

  • User283528319 posted

    Hi all,

    In SQL server if we use unsigned int or <g class="gr_ gr_52 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="52" data-gr-id="52">smallint</g> instead of signed ones can we save space?

    Is it a commonly used technique 

    Wednesday, March 13, 2019 7:14 AM

All replies

  • User475983607 posted

    In SQL server if we use unsigned int or smallint instead of signed ones can we save space?

    A SMALLINT is 2 bytes while an INT is 4 bytes.   

    Signed and unsigned refers to binary numbers that can have a negative values but are the same size in bytes.

    Use a small int if the values you are saving never exceed the max size +32,768 / -32,767.

    Wednesday, March 13, 2019 10:59 AM
  • User283528319 posted

    I was just thinking if we could get rid of the bits which represent sign.

    Wednesday, March 13, 2019 11:02 AM
  • User475983607 posted

    fatihbarut

    I was just thinking if we could get rid of the bits which represent sign.

    That's not how numbers (binary) work in a computer.  The magnitude of the number is directly related to the number of bytes.  The Most Significant Bit (MSB) is used to sigh the number.  This technique allow the number to be split in half.  An MSB of 1 means negative while 0 is positive.  

    The characters that you see on the screen is actually an encoded.  The string that represents -1234 is 5 bytes if ASCII is used but can be larger depending on the encoding.

    Anyway, that's probably more than you need to know.  I recommend, as always, reading the reference docs.

    Wednesday, March 13, 2019 11:41 AM