signed

How to perform unsigned to signed conversion in Java?

流过昼夜 提交于 2019-11-29 04:48:25
Say I read these bytes: "6F D4 06 40" from an input device. The number is a longitude reading in MilliArcSeconds format. The top bit (0x80000000) is basically always zero and is ignored for this question. I can easily convert the bytes to an unsigned integer: 1876166208 But how do I convert that unsigned value into its final form of 31-bit signed-integer? So far all I've come up with is: if value & 0x40000000 then it's actually negative, need to convert it If it's negative, strip the top bit and do something with the remaining bits... So I can tell if it's a negative number, but in order to

Is there some meaningful statistical data to justify keeping signed integer arithmetic overflow undefined?

我是研究僧i 提交于 2019-11-29 01:32:01
问题 The C Standard explicitly specifies signed integer overflow as having undefined behavior . Yet most CPUs implement signed arithmetics with defined semantics for overflow (except maybe for division overflow: x / 0 and INT_MIN / -1 ). Compilers writers have been taking advantage of the undefinedness of such overflows to add more aggressive optimisations that tend to break legacy code in very subtle ways. For example this code may have worked on older compilers but does not anymore on current

Difference between char and signed char in c++?

廉价感情. 提交于 2019-11-29 01:06:36
Consider the following code : #include <iostream> #include <type_traits> int main(int argc, char* argv[]) { std::cout<<"std::is_same<int, int>::value = "<<std::is_same<int, int>::value<<std::endl; std::cout<<"std::is_same<int, signed int>::value = "<<std::is_same<int, signed int>::value<<std::endl; std::cout<<"std::is_same<int, unsigned int>::value = "<<std::is_same<int, unsigned int>::value<<std::endl; std::cout<<"std::is_same<signed int, int>::value = "<<std::is_same<signed int, int>::value<<std::endl; std::cout<<"std::is_same<signed int, signed int>::value = "<<std::is_same<signed int,

Unsigned and Signed Values in C (Output)

橙三吉。 提交于 2019-11-28 21:56:36
signed int x = -5; unsigned int y = x; What is the value of y ? How is this so? kennytm It depends on the maximum value of the unsigned int . Typically, a unsigned int is 32-bit long, so the UINT_MAX is 2 32 − 1. The C standard (§6.3.1.3/2) requires a signed → unsigned conversion be performed as Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. Thus y = x + ((2 32 − 1) + 1) = 2 32 − 5 = 4294967291. In a 2's complement platform

Would it break the language or existing code if we'd add safe signed/unsigned compares to C/C++?

荒凉一梦 提交于 2019-11-28 21:18:46
After reading this question on signed/unsigned compares (they come up every couple of days I'd say): Signed / unsigned comparison and -Wall I wondered why we don't have proper signed unsigned compares and instead this horrible mess? Take the output from this small program: #include <stdio.h> #define C(T1,T2)\ {signed T1 a=-1;\ unsigned T2 b=1;\ printf("(signed %5s)%d < (unsigned %5s)%d = %d\n",#T1,(int)a,#T2,(int)b,(a<b));}\ #define C1(T) printf("%s:%d\n",#T,(int)sizeof(T)); C(T,char);C(T,short);C(T,int);C(T,long); int main() { C1(char); C1(short); C1(int); C1(long); } Compiled with my

In C, why is “signed int” faster than “unsigned int”?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:02:50
问题 In C, why is signed int faster than unsigned int ? True, I know that this has been asked and answered multiple times on this website (links below). However, most people said that there is no difference. I have written code and accidentally found a significant performance difference. Why would the "unsigned" version of my code be slower than the "signed" version (even when testing the same number)? (I have a x86-64 Intel processor). Similar links Faster comparing signed than unsigned ints

What is zero-width bit field [duplicate]

与世无争的帅哥 提交于 2019-11-28 19:36:09
Possible Duplicate: Practical Use of Zero-Length Bitfields Why some structures have zero-width bit fields, and why is it required? struct foo { int a:3; int b:2; int :0; // Force alignment to next boundary. int c:4; int d:3; }; int main() { int i = 0xFFFF; struct foo *f = (struct foo *)&i; printf("a=%d\nb=%d\nc=%d\nd=%d\n", f->a, f->b, f->c, f->d); return 0; } The output of above program is manav@os-team:~/programs/test$ ./a.out a=-1 b=-1 c=-8 d=0 Please explain why these values are negative, and the memory layout of these variables inside the structure? From this first hit on a Google search

How do I convert hex string into signed integer?

混江龙づ霸主 提交于 2019-11-28 13:28:07
I'm getting a hex string that needs to be converted to a signed 8-bit integer. Currently I'm converting using Int16/Int32, which will obviously not give me a negative value for an 8-bit integer. If I get the value 255 in Hex, how do I convert that to -1 in decimal? I assume I want to use an sbyte, but I'm not sure how to get that value in there properly. You can use Convert.ToSByte For example: string x = "aa"; sbyte v = Convert.ToSByte(x, 16); // result: v = 0xAA or -86 You can also use sbyte.Parse For example: string y = "bb"; sbyte w = sbyte.Parse(y, System.Globalization.NumberStyles

signed applet gives AccessControlException: access denied, when calling from javascript

本秂侑毒 提交于 2019-11-28 12:36:29
I have an easy self-signed an applet (done with keytool and the jarsigner): public class NetAppletLauncher extends JApplet { private static final long serialVersionUID = 1L; public void init() { exec("notepad c:/hello.txt"); } public void exec(String command) { try { // launch EXE and grab stdin/stdout and stderr Process process = Runtime.getRuntime().exec(command); // OutputStream stdin = process.getOutputStream(); InputStream stderr = process.getErrorStream(); InputStream stdout = process.getInputStream(); // "write" the parms into stdin // stdin.write(arguments.getBytes()); // stdin.flush()

Has anyone got any code to call SignerSignEx from C#?

天大地大妈咪最大 提交于 2019-11-28 00:53:07
Would really appreciate something that does the .Net equivalent of the SignerSignEx example here: http://blogs.msdn.com/b/alejacma/archive/2008/12/11/how-to-sign-exe-files-with-an-authenticode-certificate-part-2.aspx?CommentPosted=true Thanks!!!!!!! I got it working. If anyone's interested, here's the code - it probably needs a little more work to make it production-ready, but it works for me :) using System; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; namespace FingerPrinting.PatchUploader { internal static class Signer { #region Structures