Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

81%
+7 −0
Q&A Making code MISRA C compliant makes code less readable.

I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document: ...

1 answer  ·  posted 6mo ago by Carl‭  ·  last activity 5mo ago by wizzwizz4‭

Question c misra-c
#3: Post edited by user avatar Carl‭ · 2026-03-28T13:35:56Z (6 months ago)
  • I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document:
  • > **Rule 10.3** The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.
  • This occurs every time I alter the contents of an 8-bit register with something like ```some_register |= (1u << BIT0); ```, because the result of ``` (1u << BIT0)``` is of type unsigned int, but ```some_register``` is a uint8_t.
  • The solution I've found is to cast ``` (1u << BIT0);``` to a ```uint8_t``` but at the cost of making the code less readable.
  • ```c
  • uint8_t some_register = 0;
  • some_register |= (1u << BIT0); //Not MISRA compliant. Compiler implicitly narrows
  • some_register |= (uint8_t)(1u << BIT0); //MISRA compliant but less readable
  • some_register |= static_cast<uint8_t>(1u << BIT0); //Compliant, even less readable
  • ```
  • **Question**: To obtain MISRA compliance, do I seriously have to cast to ```(uint8_t)``` every time I want to alter the contents of my 8-bit registers? Is there a way to do this that doesn't decrease the readability of my code that much?
  • I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document:
  • > **Rule 10.3** The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.
  • This occurs every time I alter the contents of an 8-bit register with something like ```some_register |= (1u << BIT0);```, because the result of ``` (1u << BIT0)``` is of type unsigned int, but ```some_register``` is a uint8_t.
  • The solution I've found is to cast ``` (1u << BIT0);``` to a ```uint8_t``` but at the cost of making the code less readable.
  • ```c
  • uint8_t some_register = 0;
  • some_register |= (1u << BIT0); //Not MISRA compliant. Compiler implicitly narrows
  • some_register |= (uint8_t)(1u << BIT0); //MISRA compliant but less readable
  • some_register |= static_cast<uint8_t>(1u << BIT0); //Compliant, even less readable
  • ```
  • **Question**: To obtain MISRA compliance, do I seriously have to cast to ```(uint8_t)``` every time I want to alter the contents of my 8-bit registers? Is there a way to do this that doesn't decrease the readability of my code that much?
#2: Post edited by user avatar Carl‭ · 2026-03-28T13:35:46Z (6 months ago)
  • I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document:
  • > **Rule 10.3** The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.
  • This occurs every time I alter the contents of an 8-bit register with something like ```some_register |= (1u << BIT0); ```, because the result of ``` (1u << BIT0);``` is of type unsigned int, but ```some_register``` is a uint8_t.
  • The solution I've found is to cast ``` (1u << BIT0);``` to a ```uint8_t``` but at the cost of making the code less readable.
  • ```c
  • uint8_t some_register = 0;
  • some_register |= (1u << BIT0); //Not MISRA compliant. Compiler implicitly narrows
  • some_register |= (uint8_t)(1u << BIT0); //MISRA compliant but less readable
  • some_register |= static_cast<uint8_t>(1u << BIT0); //Compliant, even less readable
  • ```
  • **Question**: To obtain MISRA compliance, do I seriously have to cast to ```(uint8_t)``` every time I want to alter the contents of my 8-bit registers? Is there a way to do this that doesn't decrease the readability of my code that much?
  • I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document:
  • > **Rule 10.3** The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.
  • This occurs every time I alter the contents of an 8-bit register with something like ```some_register |= (1u << BIT0); ```, because the result of ``` (1u << BIT0)``` is of type unsigned int, but ```some_register``` is a uint8_t.
  • The solution I've found is to cast ``` (1u << BIT0);``` to a ```uint8_t``` but at the cost of making the code less readable.
  • ```c
  • uint8_t some_register = 0;
  • some_register |= (1u << BIT0); //Not MISRA compliant. Compiler implicitly narrows
  • some_register |= (uint8_t)(1u << BIT0); //MISRA compliant but less readable
  • some_register |= static_cast<uint8_t>(1u << BIT0); //Compliant, even less readable
  • ```
  • **Question**: To obtain MISRA compliance, do I seriously have to cast to ```(uint8_t)``` every time I want to alter the contents of my 8-bit registers? Is there a way to do this that doesn't decrease the readability of my code that much?
#1: Initial revision by user avatar Carl‭ · 2026-03-28T13:35:00Z (6 months ago)
Making code MISRA C compliant makes code less readable.
I've started rewriting some of my old C-code projects such that they comply with the MISRA C standards. I've noticed that literally all of them break rule 10.3 from the C:2012 guideline document:
> **Rule 10.3** The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.

This occurs every time I alter the contents of an 8-bit register with something like ```some_register |= (1u << BIT0); ```, because the result of ``` (1u << BIT0);``` is of type unsigned int, but ```some_register``` is a uint8_t.

The solution I've found is to cast ``` (1u << BIT0);``` to a ```uint8_t``` but at the cost of making the code less readable.

```c

uint8_t some_register = 0;
some_register |= (1u << BIT0); //Not MISRA compliant. Compiler implicitly narrows

some_register |= (uint8_t)(1u << BIT0); //MISRA compliant but less readable

some_register |= static_cast<uint8_t>(1u << BIT0); //Compliant, even less readable
``` 

**Question**: To obtain MISRA compliance, do I seriously have to cast to ```(uint8_t)``` every time I want to alter the contents of my 8-bit registers? Is there a way to do this that doesn't decrease the readability of my code that much?