473,385 Members | 1,400 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,385 software developers and data experts.

sub-int types, casts, MISRA and RH's writings

MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type "as
if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
On the third hand (yes, I am a coding monkey), I could try to wrap this
ugliness in macros, like
#define COMPLEMENT16(x) ((uint16_t)(~(x)))
but it doesn't look much more readable.

I am looking for an advice on how to go about this, generic if possible.
In particular, should a MISRA-inspired company's C coding standard
adjust to the skills of the team members?
A rule like "F(x,y,2007) is always 0. Why? One, Two, Three! If you don't
know the answer, abide by MISRA Rule P.Q" sounds quite idiotic... For
the author and the maintainer may be different people.
Is there a better way?
--
Thanks,
Ark
Nov 8 '07 #1
6 2474
Ark Khasin <ak*****@macroexpressions.comwrites:
MISRA came up with those "underlying types" of sub-int size (like
likely char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type
"as if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
I can't see the difference. Surely these two are the same?
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
Here you either have to cast the expression or compare it to the
result you actually expect:

if ((a ^ ~b) == ~(uint16_t)-1)

another way would be

if ((a | b) == (uint16_t)-1)

both of which have the merit that the constant is probably already
named UINT16_MAX:

if ((a ^ ~b) == ~UINT16_MAX)
if ((a | b) == UINT16_MAX)

(I prefer the second.)

--
Ben.
Nov 8 '07 #2
Ark Khasin wrote:
MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type "as
if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
I would write
if (a == (uint16_t)~b) ...
--
Thad
Nov 8 '07 #3
Ark Khasin wrote:
MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
Basically, I need to continually cast back to the "underlying" type "as
if" the computations were done on them without promotion. E.g.,
uint16_t a, b;
a = (uint16_t)(~b); //RH would not approve of it
instead of
a = ~b;
The only difference is that the first version is wordier.
Both perform the same computation. Both are vulnerable (in
theory, anyhow) to generating traps for invalid `int'
representations. One way to avoid even that vulnerability is

a = ~(unsigned int)b;
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
On the other hand, there are cases where the lack of a cast does not
implement the programmer's intentions, as in
if((a^~b)==0U) ... (always false because of upper bits of ~b)
Casting seems like a good idea, but I'd also suggest
rewriting the test for clarity:

if (a == (uint16_t)~b)

or for perfect safety

if (a == (uint16_t)~(unsigned int)b)
On the third hand (yes, I am a coding monkey), I could try to wrap this
ugliness in macros, like
#define COMPLEMENT16(x) ((uint16_t)(~(x)))
but it doesn't look much more readable.
I'm not familiar with the MISRA guidelines, but it seems
to me that if they define a "whole arithmetic" for narrow types,
there may well be a standard or at least quasi-standard suite
of functions and macros to support that arithmetic. You might
want to spend some time looking for an existing wheel (doesn't
MISRA have its roots in the auto industry?) rather than spend
your effort inventing a new one.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 8 '07 #4
On Nov 8, 5:26 pm, Ark Khasin <akha...@macroexpressions.comwrote:
MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
I think it is better to remember "int16_t may be
smaller than int, and expressions are usually
promoted to int when used with operators", than
a whole bunch of rules about when to cast and so on.

Nov 8 '07 #5
Old Wolf wrote:
On Nov 8, 5:26 pm, Ark Khasin <akha...@macroexpressions.comwrote:
>MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.

I think it is better to remember "int16_t may be
smaller than int, and expressions are usually
promoted to int when used with operators", than
a whole bunch of rules about when to cast and so on.
But of course it is! - for the programmer. Opinions vary on what goes
into company's coding standard, and as I am sure you know, people, even
skilled ones, forget things in the heat of coding.
--
Ark
Nov 8 '07 #6
On Nov 9, 12:25 pm, Ark Khasin <akha...@macroexpressions.comwrote:
Old Wolf wrote:
On Nov 8, 5:26 pm, Ark Khasin <akha...@macroexpressions.comwrote:
MISRA came up with those "underlying types" of sub-int size (like likely
char and perhaps short) and the whole arithmetic on them.
On the one hand, I find myself utterly unable to read my own
MISRA-compliant code written an hour ago.
I think it is better to remember "int16_t may be
smaller than int, and expressions are usually
promoted to int when used with operators", than
a whole bunch of rules about when to cast and so on.

But of course it is! - for the programmer. Opinions vary on what goes
into company's coding standard, and as I am sure you know, people, even
skilled ones, forget things in the heat of coding.
Yes; but if they can forget about int promotion,they can surely
also forget the rule about having to place a cast in a
comparison operation.

Nov 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Dewbert | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello, I have trouble passing a folder object (from a FileSystemObject) to a sub procedure. Consider the following code: ...
2
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
5
by: Colleyville Alan | last post by:
I have a sub that can do two different tasks based on the value of one variable. So I'd like to have two different buttons on the same form run this, but each one setting a flag so that the...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
1
by: dBNovice | last post by:
Please help! I have 3 forms: Task, Subtask, Elements. Elements is a subform of Subtask and Subtask is a subform of Task. I am able to navigate from Task to Subform to Element and from Element to...
6
by: Bob | last post by:
Hi, I found this code here below (about cartitems and shoppingcart) and I have two questions about sub New(). In the first class CartItem, there is two times sub New(): Public Sub New() End...
6
by: Greg Strong | last post by:
Hello All, Is is possible to use an ADO recordset to populate an unbound continuous Subform? I've done some Googling without much luck, so this maybe impossible, but let me try to explain...
6
by: Thom Little | last post by:
Using C# 3.5 I have a form that calls many other sub-forms. Typically there will be five forms open at the same time. If the main form is closed all the sub forms are also closed. Is there...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.