Questions tagged [signedness]

33 questions
73
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
  • 860
  • 1
  • 7
  • 8
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
  • 9,487
  • 12
  • 61
  • 89
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
  • 5,745
  • 9
  • 46
  • 65
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
9
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
  • 21,122
  • 47
  • 154
  • 286
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
  • 348,637
  • 121
  • 546
  • 611
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
  • 684
  • 1
  • 6
  • 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,261
  • 1
  • 14
  • 32
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
  • 25,576
  • 9
  • 60
  • 130
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
  • 28,030
  • 13
  • 86
  • 146
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
  • 5
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
  • 485
  • 1
  • 3
  • 8
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,360
  • 1
  • 11
  • 26
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
  • 328,932
  • 68
  • 689
  • 800
3
votes
3 answers

Why do fstream.read and fstream.write use char and not unsigned char?

As far as I know, read() and write() are there so we can read and write bytes directly from or to a file, and I was taught that the equivalent of a byte in c++ is unsigned char, so why do they take char pointers as parameters? Also, do take a look…
Pilpel
  • 2,931
  • 3
  • 25
  • 57
1
2 3