Skip to main content
GameDev.net gamedev.net
🔒 Locked

Unusual failure?

Started by AcoGL Nov 7, 2004 at 1:55 PM 0 replies 350+ views
Original Post
AcoGL
AcoGL
Hi! I make program in Linux and I have found little big problem! This is a section of code that make problems: ... char *data; data = new char[500]; ... // The data is here filled ... if (data[30]&0x0F==0x0F) { /* Here is the problem: I know that on position data[30] is number 33 (0x21 in hex), but even this the program jumps here - and it is well known that he shouldn't. What causes this? In what circumstances can this happen? Have anyone an idea? Or just link to webpage where I can find the answer? */ } ... Help me! [Edited by - AcoGL on November 11, 2004 8:32:33 AM]
Miserable
Miserable
The problem is that operator== has higher priority than operator&, so you are effectively evaluating
if(data[30] & (0x0F == 0x0F))
which is obviously true, instead of, as you want,
if((data[30] & 0x0F) == 0x0F)
which in your case would be false. Parenthesise accordingly.

In fact, as bitwise operators tend to catch people by surprise (for some reason, people—myself included—seem inclined to think that their priority is higher than it truly is), I think it may be a good idea to always fully parenthesise bitwise operations (whether bitwise logic or shifts).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.