signedness

signed int modulo unsigned int produces nonsense results

∥☆過路亽.° 提交于 2020-06-15 20:28:45
问题 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 system. But it makes no sense to allow negative module, therefore i wrote unsigned int mod( int x, unsigned int m ) { int r = x % m; return r >= 0 ? r : r + m; } However calling such function with negative number and positive module printf("%u\n", mod(

Is the signedness of char an interface issue?

北战南征 提交于 2019-12-12 12:12:53
问题 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 declaration to void foo(unsigned char *) Now, given that char , signed char and unsigned char are three different types, would this constitute an interface change, under any reasonable definition of the term "interface" in C? (This question is

Difference between C# and java big endian bytes using miscutil

扶醉桌前 提交于 2019-12-10 23:57:19
问题 I'm using the miscutil library to communicate between and Java and C# application using a socket. I am trying to figure out the difference between the following code (this is Groovy, but the Java result is the same): import java.io.* def baos = new ByteArrayOutputStream(); def stream = new DataOutputStream(baos); stream.writeInt(5000) baos.toByteArray().each { println it } /* outputs - 0, 0, 19, -120 */ and C#: using (var ms = new MemoryStream()) using (EndianBinaryWriter writer = new

Can someone explain how the signedness of char is platform specific?

十年热恋 提交于 2019-12-10 02:09:41
问题 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 bit, platform2 the sign could be at the end? how would you code against this? Basically my question comes from seeing this line: typedef unsigned char byte; I dont understand the relevance of the signage? 回答1: Let's assume that your platform has

When does the signedness of an integer really matter?

限于喜欢 提交于 2019-12-07 10:36:21
问题 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 >>= 2; printf("%u",u); // -> 2 printf("%u",i); // -> 2 So, is there a set of rules to tell under which conditions the signedness of a variable really makes a difference? 回答1: It matters in these contexts: division and modulo: -2/2 = 1 , -2u/2 = UINT_MAX/2-1 , -3%4 = -3 , -3u%4 =

For any finite floating point value, is it guaranteed that x - x == 0?

谁说胖子不能爱 提交于 2019-12-06 22:51:43
问题 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 compare results of floating point calculations is to see if the absolute difference against some expected value is less than some tolerated epsilon. System.out.println(Math.abs(.1 + .2 - .3) < .00000000000001); // true The question is about whether or

When does the signedness of an integer really matter?

独自空忆成欢 提交于 2019-12-05 12:32:56
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 >>= 2; printf("%u",u); // -> 2 printf("%u",i); // -> 2 So, is there a set of rules to tell under which conditions the signedness of a variable really makes a difference? It matters in these contexts: division and modulo: -2/2 = 1 , -2u/2 = UINT_MAX/2-1 , -3%4 = -3 , -3u%4 = 1 shifts. For negative signed values, the result of >> and << are implementation defined or undefined, resp

Can someone explain how the signedness of char is platform specific?

自作多情 提交于 2019-12-05 01:38:30
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 bit, platform2 the sign could be at the end? how would you code against this? Basically my question comes from seeing this line: typedef unsigned char byte; I dont understand the relevance of the signage? Let's assume that your platform has eight-bit bytes, and suppose we have the bit pattern 10101010 . To a signed char , that value is −86. For

Sign of a floating point number

别来无恙 提交于 2019-11-30 18:41:04
Is there an easy way to determine the sign of a floating point number? I experimented and came up with this: #include <iostream> int main(int argc, char** argv) { union { float f; char c[4]; }; f = -0.0f; std::cout << (c[3] & 0x10000000) << "\n"; std::cin.ignore(); std::cin.get(); return 0; } where (c[3] & 0x10000000) gives a value > 0 for a negative number but I think this requires me to make the assumptions that: The machine's bytes are 8 bits big a float point number is 4 bytes big? the machine's most significant bit is the left-most bit (endianness?) Please correct me if any of those

Sign of a floating point number

。_饼干妹妹 提交于 2019-11-30 02:47:17
问题 Is there an easy way to determine the sign of a floating point number? I experimented and came up with this: #include <iostream> int main(int argc, char** argv) { union { float f; char c[4]; }; f = -0.0f; std::cout << (c[3] & 0x10000000) << "\n"; std::cin.ignore(); std::cin.get(); return 0; } where (c[3] & 0x10000000) gives a value > 0 for a negative number but I think this requires me to make the assumptions that: The machine's bytes are 8 bits big a float point number is 4 bytes big? the