short

2 Chars to Short in C

扶醉桌前 提交于 2021-02-19 00:21:18
问题 I've got 2 chars. Char 128 and Char 2 . How do I turn these chars into the Short 640 in C? I've tried unsigned short getShort(unsigned char* array, int offset) { short returnVal; char* a = slice(array, offset, offset+2); memcpy(&returnVal, a, 2); free(a); return returnVal; } But that didn't work, it just displays it as 128 . What's the preferred method? 回答1: I found that the accepted answer was nearly correct, except i'd run into a bug where sometimes the top byte of the result would be 0xff

Bit manipulation in negative byte and short data types in Java

时光怂恿深爱的人放手 提交于 2020-01-16 20:21:48
问题 Im trying to implement a class that stores a 32-bit number without using the int primitive type. For doing so, I'm using two short variables msbs and lsbs to store the 32 bits of the number, 16 bits in each variable. The variable msbs will store the first 16 bits of the number and the lsbs variable the 16 bits left. When It comes to save the given bytes to the variables I apply the next formula: (The bytes order are given as Little-Endian notation) Input -> byte[] n = {0b00110101, -3, 0b1001,

Bit manipulation in negative byte and short data types in Java

别等时光非礼了梦想. 提交于 2020-01-16 20:21:21
问题 Im trying to implement a class that stores a 32-bit number without using the int primitive type. For doing so, I'm using two short variables msbs and lsbs to store the 32 bits of the number, 16 bits in each variable. The variable msbs will store the first 16 bits of the number and the lsbs variable the 16 bits left. When It comes to save the given bytes to the variables I apply the next formula: (The bytes order are given as Little-Endian notation) Input -> byte[] n = {0b00110101, -3, 0b1001,

Android: does short take really 2 bytes?

江枫思渺然 提交于 2020-01-14 09:52:28
问题 I am trying to make decision how to design my app. I have about 300 instances of class just like this: public class ParamValue { protected String sValue = null; protected short shValue = 0; protected short mode = PARAM_VALUE_MODE_UNKNOWN; /* * ... */ } I have an array of these instances. I can't find out does these shorts really take 2 bytes or they take anyway 4 bytes? And i need to pass the list of these objects via AIDL as a List<Parcelable> . Parcel can't readShort() and writeShort() , it

How do I get the high- and low-order bits of a SHORT?

自闭症网瘾萝莉.ら 提交于 2020-01-14 09:00:13
问题 The function GetKeyState() returns a SHORT that contains the key's state (up/down in the high-order bit, and toggled in the low-order). How do I get those values? 回答1: Simple bit manipulation will work. SHORTs are 16-bit integers, so to get the low- and high-order bits you can do the following: lowBit = value & 1; highBit = ((unsigned short) value) >> 15; Also, note that the LOBYTE and HIBYTE macros are used to break SHORTs into low- and high-order bytes , not to test individual bits in a

Jogl, creating only red channel u16 but getting “Texture type and format combination is not valid”

ぐ巨炮叔叔 提交于 2020-01-06 04:54:17
问题 So, I am trying to implement the picking through id. This means with every drawArray a different unique id will be set as uniform and saved in the red component on a texture. 16 bits are more than enough (65k elements), so I choose to use shorts, I know that uniform variable can be only ui, but I decided to gave it a try anyway I also found another question, here, where the answer contains a small example with shorts However here my code to initialize the framebuffer and two textures, one for

Convert int to unsigned short java

浪子不回头ぞ 提交于 2020-01-03 10:56:05
问题 I have written a .obj parser in java to modelize 3D objects on iPhone. I would like to export the data as a binary file, which must be as small as possible. I have plenty of indices that would fit a unsigned short, but they are represented as int in java. I would like to use the ByteBuffer class to do the conversion just before writing the data in a file. I suppose I will have to manipulate bytes before pushing them into the ByteBuffer but I have no idea how to do so. Thank you in advance if

convert float to short with minimal loss of precision [closed]

不问归期 提交于 2020-01-02 05:46:44
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I have this sine wave which generates floating point values (e.g. 0.37885) but I want them as shorts. Direct casting with short gives me a value of 0. so what is the solution? Can anyone tell me how to do it - ideally without loss of precision - or minimal loss of precision if this is all that is

Behavior of int and short in c

限于喜欢 提交于 2019-12-31 01:59:10
问题 I want to know what is the reason of the output of the following codes: unsigned short a=10,aa=-1; if(a>-1) printf("surprise"); else printf(" No surprise"); This gives output "Surprise" unsigned int a=10,aa=-1; if(a>-1) printf("surprise"); else printf("No surprise"); This gives output "No Surprise" and unsigned short a=10,aa=-1; if(a>aa) printf("surprise"); else printf("No surprise"); This gives the output "No Surprise" 回答1: See this Stack Exchange question: In a C expression where unsigned

possible lossy conversion from int to short

巧了我就是萌 提交于 2019-12-31 00:57:11
问题 I have defined array gx, array arr to be short type. but why the operation at left may end up with int type and I must cast it into short? the compiler error is possible lossy conversion from int to short. this is my code. public PixImage sobelEdges() { short gy=0; for(int x=1;x<width-1;x++){ for(int y=1;y<height-1;y++){ // if(x=){ for(int z=0;z<3;z++){ gx[x][y][z]=arr[x-1][y-1][z]-arr[x+1][y-1][z]+2*arr[x-1][y][z]-2*arr[x+1][y][z]+arr[x-1][y+1][z]-arr[x+1][y+1][z]; } // } } } return this; //