Re: C and MISRA, blues... (arithmetic shifts)
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 25 Apr 2008 09:38:03 -0400
Hans-Bernhard Bröker wrote:
Nils wrote:
But do you do if you need them anyway?
Check very thoroughly why you think you need them. Shifting is
not really a numerical operation, so why should something you
describe as "numerical code" need it in the first place?
Shifting IS a numerical operation. Read the following description,
from the C standard. Notice the 'undefined' cases for negative
operands.
6.5.7 Bitwise shift operators
Syntax
[#1]
shift-expr:
additive-expr
shift-expr << additive-expr
shift-expr >> additive-expr
Constraints
[#2] Each of the operands shall have integer type.
Semantics
[#3] The integer promotions are performed on each of the
operands. The type of the result is that of the promoted
left operand. If the value of the right operand is negative
or is greater than or equal to the width of the promoted
left operand, the behavior is undefined.
[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1+2E2, reduced
modulo one more than the maximum value representable in the
result type. If E1 has a signed type and nonnegative value,
and E1+2E2 is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.
[#5] The result of E1 >> E2 is E1 right-shifted E2 bit
positions. If E1 has an unsigned type or if E1 has a signed
type and a nonnegative value, the value of the result is the
integral part of the quotient of E1 divided by the quantity,
2 raised to the power E2. If E1 has a signed type and a
negative value, the resulting value is implementation-
defined.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
.
- References:
- C and MISRA, blues... (arithmetic shifts)
- From: Nils
- Re: C and MISRA, blues... (arithmetic shifts)
- From: Hans-Bernhard Bröker
- C and MISRA, blues... (arithmetic shifts)
- Prev by Date: Re: C and MISRA, blues... (arithmetic shifts)
- Next by Date: Re: AC spike go pass XFRM and regulator to reset CPU!!??
- Previous by thread: Re: C and MISRA, blues... (arithmetic shifts)
- Next by thread: Re: C and MISRA, blues... (arithmetic shifts)
- Index(es):