Questions tagged [signedness]
37 questions
74
votes
5 answers
Type conversion - unsigned to signed int/char
I tried the to execute the below program:
#include
int main() {
signed char a = -5;
unsigned char b = -5;
int c = -5;
unsigned int d = -5;
if (a == b)
printf("\r\n char is SAME!!!");
else
…

user2522685
- 870
- 1
- 7
- 8
13
votes
7 answers
how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?
I am reading the book: CS-APPe2. C has unsigned and signed int type and in most architectures uses two's-complement arithmetic to implement signed value; but after learning some assembly code, I found that very few instructions distinguish between…

tomwang1013
- 1,349
- 2
- 13
- 27
12
votes
8 answers
Worst side effects from chars signedness. (Explanation of signedness effects on chars and casts)
I frequently work with libraries that use char when working with bytes in C++. The alternative is to define a "Byte" as unsigned char but that not the standard they decided to use. I frequently pass bytes from C# into the C++ dlls and cast them to…

QueueHammer
- 10,515
- 12
- 67
- 91
12
votes
6 answers
Can someone explain how the signedness of char is platform specific?
I recently read that the differences between
char
unsigned char
and
signed char
is platform specific.
I can't quite get my head round this? does it mean the the bit sequence can vary from one platform to the next ie platform1 the sign is the first…

Adam Naylor
- 6,172
- 10
- 49
- 69
10
votes
9 answers
Sign of a floating point number
Is there an easy way to determine the sign of a floating point number?
I experimented and came up with this:
#include
int main(int argc, char** argv)
{
union
{
float f;
char c[4];
};
f = -0.0f;
std::cout << (c[3] & 0x10000000)…

Rarge
- 221
- 1
- 4
- 13
10
votes
1 answer
Should I use "unsigned" every time i know I'm processing unsigned values?
Often values are known to be positive. For example TCP/UDP sequence number is always positive value. Both int and unsigned int are big enough to store even the biggest sequence number so I can use any of these types. There are many other examples…

Oleg Vazhnev
- 23,239
- 54
- 171
- 305
8
votes
2 answers
For any finite floating point value, is it guaranteed that x - x == 0?
Floating point values are inexact, which is why we should rarely use strict numerical equality in comparisons. For example, in Java this prints false (as seen on ideone.com):
System.out.println(.1 + .2 == .3);
// false
Usually the correct way to…

polygenelubricants
- 376,812
- 128
- 561
- 623
6
votes
1 answer
How do I #define an unsigned char* string?
I have following define in my code
#define PRODUCTNAME "SomeName"
and I want to send it with a function com_reply(unsigned char* msg, uint16_t lenght).
Now I get a warning that my argument differs in signedness. I know what the problem is and…

Dehalion
- 757
- 1
- 7
- 16
5
votes
1 answer
In C, for example, why is second operand of shift allowed to be signed?
Note: This question is all about the signedness of the second operand of bit shift operators << and >>. Not at all about the first operand.
CERT INT34-C, in part: Do not shift a negative number of bits ...
Not that it needed justification, but they…

talkaboutquality
- 1,312
- 2
- 16
- 34
5
votes
1 answer
Right-shifting 32-bit ints
Clojure's bit-shift operations all seem to return 64-bit long results, even for 32-bit int arguments. This is not a substantial problem for bit-shift-left:
user=> (format "%08x" (unchecked-int (bit-shift-left (unchecked-int 0x12345678)…

Cactus
- 27,075
- 9
- 69
- 149
4
votes
6 answers
When does the signedness of an integer really matter?
Due to the way conversions and operations are defined in C, it seems to rarely matter whether you use a signed or an unsigned variable:
uint8_t u; int8_t i;
u = -3; i = -3;
u *= 2; i *= 2;
u += 15; i += 15;
u >>= 2; i >>=…

AndreKR
- 32,613
- 18
- 106
- 168
4
votes
1 answer
Function return type is unsigned while it returns -1 for error
I've been using libnet for a while and I've noticed there are some functions which return value is uint32_t which, from my understanding, is a unsigned type. However, in the documentation, it says to return -1 if an error occurred(which is a signed…

DrNoob
- 75
- 1
- 7
4
votes
3 answers
Conflicting signs in x86 assembly: movsx then unsigned compare/branch?
I am confused in the following snippet:
movsx ecx, [ebp+var_8] ; signed move
cmp ecx, [ebp+arg_0]
jnb short loc_401027 ; unsigned jump
This seems to conflict. Var_8 appears to be signed on the account of it getting sign-extended. Yet, jnb…

ineedahero
- 715
- 1
- 5
- 10
4
votes
3 answers
signed int modulo unsigned int produces nonsense results
I need to perform a real mathematical modulo in C. It makes sense for me to allow negative numbers for the moduled argument, since my modular calculations can produce negative intermediate results, which must be put back into the least residue…

Youda008
- 1,788
- 1
- 17
- 35
4
votes
5 answers
Is the signedness of char an interface issue?
Suppose I have a function
void foo(char *)
which, internally, needs to treat its input as a block of NUL-terminated bytes (say, it's a hash function on strings). I could cast the argument to unsigned char* in the function. I could also change the…

Fred Foo
- 355,277
- 75
- 744
- 836