Re:Should I Use Signed or Unsigned Ints? (Part 2)
by alricb on Tuesday September 01, 2015 @08:44PM
(#10156792)
Honest question: Would it be possible to create a "friendly" version of signed that is actually unsigned underneath? After all, it's just bit patterns.
[ Parent
| on HN ]
Re:Should I Use Signed or Unsigned Ints? (Part 2)
by detrino on Tuesday September 01, 2015 @10:48PM
(#10157164)
On modern computers, both unsigned/signed ints are just bit patterns and it's only some operations that differ. add/sub/mul/left-shift are the same, but cmp/div/right-shift differ. Look up twos-complement representation for more information.
Re:Should I Use Signed or Unsigned Ints? (Part 2)
by Sanddancer on Tuesday September 01, 2015 @11:26PM
(#10157268)
Left shift also differs. Raw shifting left can have the affect of turning a negative number positive by setting the sign bit to 0, so there's a sign maintaining left shift as well. Also, Left/Right shift of negative numbers have the opposite effect as they do with positive, with left shifting a negative number dividing and right shifting multiplying.
Re:Should I Use Signed or Unsigned Ints? (Part 2)
by detrino on Wednesday September 02, 2015 @12:34AM
(#10157440)
You are mistaken, left shift is the same for both signed and unsigned and any instance of a left shift turning a signed int into a positive number is just normal underflow.
Re:Should I Use Signed or Unsigned Ints? (Part 2)
by Dylan16807 on Tuesday September 01, 2015 @09:36PM
(#10156961)
fwrapv?