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

Negative values for shift operators.

K&R states that the right operand must be non-negative.

"The shift operators << and >> perform left and right shifts of their
left operand by the number of bit positions given by the right operand,
which must be non-negative"

unsigned int n = 10;
n <<= -2;

MSVC7 accepts this above code as valid and compiles it.

Should this be allowed, it seems like it could lead to subtle bugs?

rcn

Nov 15 '05 #1
5 5645
ri************@gmail.com wrote:
K&R states that the right operand must be non-negative.

"The shift operators << and >> perform left and right shifts of their
left operand by the number of bit positions given by the right operand,
which must be non-negative"

unsigned int n = 10;
n <<= -2;

MSVC7 accepts this above code as valid and compiles it.

Should this be allowed, it seems like it could lead to subtle bugs?


The behavior of the shift is undefined, meaning that
the C Standard makes no guarantees about what might happen
if the program attempts to evaluate the offending expression.
"Anything can happen" includes as a special case right-shifting
`n' by two bit positions -- but it also includes left-shifting
by thirty positions, not shifting at all, or causing your CPU
to overheat and melt.

So, yes: it could lead to bugs, subtle and unsubtle, just
like dividing by zero or writing to the [105] position of a
three-element array.

However, the Standard does not require the compiler to
catch every misteak in the program. Some mistakes must be
caught (in the sense that the compiler is required to produce
a diagnostic message), but not all. Some compilers will catch
more mistakes than they are required to; some will even issue
warnings about valid but "suspicious" code. It appears that
the compiler you are using doesn't catch this particular mistake;
it's not among the "must catch" mistakes, so that's permissible.
Perhaps the compiler supports different "warning levels" and
would catch this mistake if you increased its "sensitivity;"
check the documentation for the compiler to see if there's a
way to get it to be pickier.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2
<ri************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
K&R states that the right operand must be non-negative.

"The shift operators << and >> perform left and right shifts of their
left operand by the number of bit positions given by the right operand,
which must be non-negative"

unsigned int n = 10;
n <<= -2;

MSVC7 accepts this above code as valid and compiles it.

Should this be allowed, it seems like it could lead to subtle bugs?


Just because MSVC7 did that, doesn't matter everyone else would. It is
unreasonable to think that if this particular compiler does the trick
another does too. You must never use shifts by negative amount/number of
places or you're looking for a trouble. The K&R statment is still valid,
that's what the current standard states as well. The best practice is to
avoid implementation specific things and things that may cause undefined
behaviour. If there's a doubt or discrepancy among the compilers or between
a compiler and the standard, don't do that damned thing. Just because in
many places for a pointer to data or function you see DWORD or something of
that sort, doesn't meen it's good.

Alex
Nov 15 '05 #3
.....

Nov 15 '05 #4
sv*********@yahoo.de wrote:

....


Several replies with zero content.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #5
ok

Nov 15 '05 #6

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

Similar topics

3
by: Simon Johnson | last post by:
Is there an inbuilt circular shift operator in c#? I've googled it but because c# happens to be musical note.. i get a large noise to signal ratio :P Simon.
8
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. ...
4
by: August Karlstrom | last post by:
Hi, How come the modulus operator doesn't always yield non-negative values? I expect e.g. (-1) % 3 to be 2 but I get -1. If I want to decrement a cyclic variable n by k I have to write something...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
9
by: deepak | last post by:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving the same value as that of n-sizeof(n). Why is it behaving like this. I studied that the bit 'll be replaced by '0'. ...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
16
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this...
4
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.