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
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: ...
#3: Post edited
- 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
- 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
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?
