473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Splitting uint32 to two int16 and reconstructing them again

I am trying to save a uint32 value to a modbus implementation where the registers are declared as signed short (should be the same as int16?).

Code that should represent what I am trying to do:
Expand|Select|Wrap|Line Numbers
  1. uint32_t x = 0x8000;
  2. int16_t a;
  3. int16_t b;
  4.  
  5. //Split
  6. a = x >> 16;
  7. b = x & 0x0000FFFF;
  8.  
  9. //Reconstruction
  10. x = (uint32_t)a << 16 | (uint32_t)b
  11.  
Problem is that when the number to be splitted (x) exceeds 0x8000 the 16 higest bits is 0xFFFF after reconstruction.

Would appreciate if anybody would help me understand what I have missed.
Oct 11 '10 #1
3 27521
weaknessforcats
9,208 Expert Mod 8TB
You are assuming an implementation.

The only way to do this is by using a typecast.

There is no way to stuff a 32 bit value into a 16 bit field without the possibility of losing data. The typecast tells the compiler you don't care.
Oct 11 '10 #2
You are right, the assumption that a signed short equals a uint16_t is not valid for all systems, just happens to be so in my (bad implementation of modbus interface though, one of my first real C projects).

Do I understand correctly that you claim there is no way of putting a 32-bit value in two 16-bit and then reconstruct with out risk of loosing data.

Is it not a question of correct limitations and preventing uncertainties in the compilator specification?

After more thought and reading selected chapters in C99 spec. I think this might work. Might be so that b was first promoted to a signed 32-bit (and thus sign extended) and there after interpreted as an unsigned.

Expand|Select|Wrap|Line Numbers
  1. x = (uint32_t)a << 16 | ((uint32_t)b & 0x0000FFFF);
  2.  
Oct 11 '10 #3
donbock
2,426 Expert 2GB
tomago is correct, your problem is caused by sign-extension.

At lines 6 and 7 you have what you expect:
a is 0, and b is 0x8000.

However, if perchance your implementation uses two's-complement encoding for signed integers, then b is considered to be a negative value.

Let's split line 10 into several lines so we can look at each term:
Expand|Select|Wrap|Line Numbers
  1. uint32_t xa, xb;
  2. ...
  3. xa = (uint32_t)a << 16;
  4. xb = (uint32_t)b;
  5. x = xa | xb;
Here xa is 0, but xb is 0xFFFF8000 because the sign bit is extended to the left.

By the way, are you sure the expression for xa is correct? It is if the cast has a higher precedence than the shift-left. Does it? You don't need to remember the precedence table if you use parentheses.

You can solve the sign-extension issue if your computations are always performed on unsigned values.
Expand|Select|Wrap|Line Numbers
  1. uint32_t x = 0x00008000uL;
  2. uint16_t ua, ub;
  3. int16_t = a, b;
  4.  
  5. //Split
  6. ua = (uint16_t) (x >> 16);
  7. ub = (uint16_t) (x & 0x0000FFFFuL);
  8. a = (int16_t)ua;
  9. b = (int16_t)ub;
  10.  
  11. //Reconstruction 
  12. ua = (uint16_t)a;
  13. ub = (uint16_t)b;
  14. x = (((uint32_t)ua) << 16) | ((uint32_t)ub);
Notice also the use of the uL suffix.
Oct 11 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
4
by: john smith | last post by:
I have never really understood the difference between the two notations below. I often run into code where they are passing in the address of some location. Some people do something like...
13
by: Skybuck Flying | last post by:
Hi, I would like to split main.c which contains all code into the following Step 1. Machine.h Step 2. drand.h and drand.c Step 3. nrand.h and nrand.c Main.c then only contains some test...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
1
by: Michel Racicot | last post by:
Hi there, The following code is troubling me: UInt32 nOffset = nStringEntriesOffset + (40 * nIndex); nStringEntriesOffset is a UInt32 containing 2124 nIndex is a UInt32 containing 5213 ...
0
by: Pat Ireland | last post by:
To use the InvokeMember for calling a method in a dynamically loaded class requires that any arguments to the method must be past in an object . However, when I try to perform a pass by reference...
4
by: Mark Hollander | last post by:
Hi, Is there an easier way to convert a UInt16 to Int16 than the way I am currently doing it Dim iValue As Int16 Dim uValue As UInt16 Try uValue = GetValue(WMI.NetworkAdapter,...
9
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.