简体   繁体   中英

What is the difference between signed and unsigned binary

I've been reading a few sites, but none of them make sense to me. Is signed and unsigned binary them same as signed and unsigned variables. I'd be glad if you could help :)

The "signed" indicator means that the item can hold positive or negative values. "Unsigned" doesn't distinguish between positive and negative values. A signed/unsigned variable can refer to any numerical data type (such as binary, integer, float, etc). Each data type might be further defined as signed or unsigned.

For example, an 8-bit signed binary could hold values from 0-127, both positive and negative (1 bit is used for the sign and 7 bits for the value), while an 8-bit unsigned binary could hold values from 0-255 (nothing distinguishes whether or not the value should be considered positive or negative, though it is commonly assumed to be positive).

A signed binary is a specific data type of a signed variable.

Hope that helps!

A "signed" variable means that the value holds a positive or negative value using it's most significant bit (the last bit to the left), which is what we call the "signed bit". An "unsigned" variable does not, but instead the most significant bit is just the next power of two.

We call a signed bit that is 1 a negative number whereas on an unsigned number the bit would fall under the regular binary bit rules.

For example max values look like this:
Unsigned Char 0b11111111 (0xFF in hex) = 255 in decimal, (128+64+32+16+8+4+2+1 = 255)
Signed Char 0b11111111 (0xFF in hex) = -127 in decimal, (-1 * (64+32+16+8+4+2+1) = - 127)

Additionally what you might see in code:
Unsigned Char 0b10000001 (0x81 in hex) = 129 in decimal, (128 + 1 = 129)
Signed Char 0b10000001 (0x81 in hex) = -1 in decimal, (-1 * 1)

(Note: char is one byte which means it has eight digits in binary that can be changed)
(For anyone who is wondering, 0b means the bit is in binary and 0x means it is in hex)

Signed and Unsigned Binary refers to the conversion that depends on sign of the binary represented. Whereas for the variables it refers to having the variable able to store the negative value or not.

In Binary for signed bit: We say 1 is negative and 0 is positive. So if you see second example, the first bit is 1 means? - right, its negative. And we dont include it for the conversion base2 to base10.

For example: 1001 In Unsigned bit (dont care about sign) : 9

For example: 1001 In Signed bit (MSB is a sign bit): -1

For variables is it very likely that stores negative numbers.

MSB: Most Significant Bit

It depends on the position or situation . Example,in assembly, We want to load byte have value: 0xFF(~11111111 in binary) from memory. $s3 have address of this value.

  • with func lbu( load byte unsignal ), it only allows to load unsignal binary: lb rt, offset(rs).

    • lbu $s0, 32($s3) : lbu will load value and 0-extend to 32 bit 0x000000FF which is interpreted as 255.
  • with func addi, it allows to load signal binary: lb rt, ofset(rs).
    • lb $s0, 32($s3) : lb will load value and 1-extend to 32 bit 0xFFFFFFFF which is interpreted as -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM