# Code explaination

Posted by ShaunGVW
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Code explaination October 28, 2009 08:57AM Registered: 13 years ago Posts: 523 Rating: 0
Can someone explain the code below to me?

```Language: C++static uint16 t_id = 0;
uint8_t query[14];
if (t_id < UINT16_MAX)
t_id++;
else
t_id = 0;
query[0] = t_id >> 8;```

My understanding
1. t_id set to zero
2. is t_id less then 65536? (Yes, since it was set to 0 previously)
3. increment t_id, now equal to 1
4. set query[0] equal to t_id right shift 1 by 8 bits, i.e. zero

What is the point of this whole thing? Or where am I going wrong?
 Anonymous User Re: Code explaination October 28, 2009 12:41PM Rating: 0
yeah, what is the point of that code? It looks like C, too
 Re: Code explaination October 28, 2009 01:12PM Registered: 13 years ago Posts: 523 Rating: 0
I'm working on a Modbus application, and I have this code that I am trying to understand.
So am I right in my understanding of it?
 Re: Code explaination October 28, 2009 01:15PM Registered: 13 years ago Posts: 523 Rating: 0
You want to help....????
 Re: Code explaination October 28, 2009 06:32PM Registered: 14 years ago Posts: 1,424 Rating: 0
The code needs more context to be understood properly but I can suggest that it looks like it's supposed to be in a function that is expected to be called many times.

t_id is static so it only gets initialised the first time the function is called and it retains it's value between function calls. What this means is that the variable is not destroyed when the function ends, it's only destroyed when the entire program terminates.

The designer wants the value of t_id to increment every time the function is called. It is quite feasible that the function may be called in excess of 65535 times. If this happens, the designer wants the value to be reset back to zero.

This effectively restricts t_id to numbers that range from 0 to 65535, or in other words, all the numbers that can fit into a two byte variable (which is alluded by the type 'uint16'. Taking that into consideration, you can think of the number held by t_id as a number with bits spread out across 16 bits, or two sets of 8 bits. It looks like the designer is only interested in the bits from the high byte (bits 9 to 16) and the quickest way to find out what those bits are is to chop the bits in the low byte off (bits 1 to 8), ie to shift the entire number right by 8 bits. This will only produce non-zero results once t_id increments to 256 or greater.

Sorry, I can't offer you any more explanation than that without seeing more code. I hope it turns a light bulb on somewhere.
 Re: Code explaination October 29, 2009 07:09AM Registered: 13 years ago Posts: 523 Rating: 0
I missed understanding that static initialisation. That definitely does help, thank you! And in context of the full program, it also fits.
Sorry, only registered users may post in this forum.