![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Member
Join Date: Jan 2003
Posts: 85
|
Code:
|
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
the good old trick
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
indeed. good to get the algorithms and code snippets filled with such stuff. thats what its ment for!
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#4 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
hmm... let's hope people use the search function in this forum, unlike other places we don't want to mention
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#5 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
another one
Code:
|
|
|
|
|
|
#6 | |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
Quote:
lets see if we find some bether solution to it.. would be nice..
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
|
#7 |
|
New Member
Join Date: Oct 2004
Posts: 7
|
For two more useful functions which round to the next bigger / next smaller power of two, see: http://www.flipcode.com/cgi-bin/fcarticles...show=4&id=64182
___________________________________________
http://threekings.tk/ |
|
|
|
|
|
#8 | |
|
New Member
Join Date: Jan 2006
Location: Pune, India
Posts: 2
|
Quote:
It is not going to work for if the numbers are = 1, 0, -1.... Can give you solution in that case |
|
|
|
|
|
|
#9 |
|
New Member
Join Date: Jan 2006
Location: Pune, India
Posts: 2
|
this solution will not work for if if the number = 1, 0, -1....
need some thing like return (value != 0) && (value != 1) && ( ( (value-1) & value ) == 0 ); but still need to modify |
|
|
|
|
|
#10 | |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
Quote:
Only not for zero. 1 & -1 == 1, which is correct as 1 is a power of two (2^0 to be exact). -1 & 1 is of course also 1, but now 'value' is -1 so the end result doesn't equal 'value', therefore it is not a power of two (this works for every negative value btw) Only the zero is a special case. So it would be: Code:
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
|
#11 |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 152
|
Is a bitwise "&" consistent across platforms?? Just curious...
Alex |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
It depends what language standards the compiler follows but for the most cases yes.
|
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Oct 2005
Posts: 745
|
What?
I've never heard of bitwise & not being consistent. That wouldn't be "not adhering to standards", that would be a *bug*. Unless I'm missing something. Mihail121, do you know of a compiler that doesn't handle this? I'd find it incredible to hear that there's a "standard" that defies normal bitwise &. It's like saying that my compiler has a different standard for "+".
___________________________________________
Kicking Dragon (dot) com - Technical Ramblings of a Game Programmer |
|
|
|
|
|
#14 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Well, bitwise & ought to work the same way everywhere, but not all machines are two's complement and so some of the formulas posted above will not always work. (Realistically though, any machine you are going to be writing code for nowadays uses two's complement for integer arithmetic.)
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
#15 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
Indeed, the standard requires signed ints to be either two's complement, one's complement or sign and magnitude. But that doesn't really matter, it only means we cannot use the negation operator in our test code. So either use bladder's, or this:
Code:
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#16 |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 152
|
I wonder why there is no support for "flags" that indicate what kind of system the code has been written for (little endian, pointer size, etc etc).
That way the compiler could easily "fix" the code (or rather change the output to do what we meant it to do) if the current target doesn't fit the flags. Why is it up to us to worry about all that crap? Using the flags it should be inherently clear what the code is supposed to do.. Sorry to high jack the thread but this is probably not worth starting a new one.. Alex |
|
|
|
|
|
#17 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Because C has been designed to write OS with, you're supposed to know what you're doing.
As it is the Standard is byzantine enough, it doesn't need even more assumptions. Use the right tool for a given task. If you want more abstraction from the hardware, use another language. If you want less, write some asm. |
|
|
|
|
|
#18 |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 152
|
Now I'm probably not the only one who want's to write portable C++ code. Also the feature I suggest wouldn't stop anyone from writing OS code..it's less a language thing, more of a compiler option. There is no good reason I can see to have interpretations of my code that I have no control over but which alter the logic.
Last edited by Alex : 01-31-2006 at 06:11 AM. |
|
|
|
|
|
#19 | ||
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Quote:
So either you don't want to have to handle such issues and use another language that insulates you better, or rely on something like autoconf. Quote:
The compiler only care about how your code should be interpreted vs the Standard. It doens't read into your mind yet ![]() Last edited by tbp : 01-31-2006 at 06:20 AM. |
||
|
|
|
|
|
#20 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
tbp: you are missing the point. For example, the standard states that the number of bits in a char shall be 8 or more. Fortunately, the standard also states that there shall be a CHAR_BIT constant representing the number of bits in a char. It also defines other handy constants like INT_MAX. These are all pretty implementation defined, but the point is that you have access to such constants, so you can write portable code that runs on different architectures using these constants.
Unfortunately for endianness and how signed integers are represented, there is no such thing.
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. Last edited by .oisyn : 01-31-2006 at 06:52 AM. |
|
|
|
|
|
#21 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Again, portability has never been part of the set of problems C has been designed to solve.
It's just a thin abstraction layer from the metal, some souped assembler. By design and for varied reasons like efficiency and compiler complexity. If portability is your prime concern you shouldn't be using C (or any of its descendant) in the first place. So, i'm not missing any point, you're just barking at the wrong tree. |
|
|
|
|
|
#22 |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 152
|
Bjarne Stroustrup lists C++ as a general-purpose language with a bias to "generic programming" (among others including OS stuff):
http://www.artima.com/cppsource/cpp0x.html Where "Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations." So I agree that total portability is NOT an explicit! goal of the standard. Reuseability on the other hand means that portability is desired where it doesn't hurt the standard (or the target specific constrains). Also note that at least I'm talking about C++, not C. Which language(s) to use depends on the problem at hand. I doubt that a desire for portability prohibits using C++. ALex |
|
|
|
|
|
#23 | |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Portability isn't and never was an explicit goal of the C++ Standard for a good reason, let me cite the prophet himself:
Quote:
You can talk genericity and OOness all day long, C++ is by design compatible with C. Thusly, regarding portability they both stand side by side. |
|
|
|
|
|
|
#24 | |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 152
|
Quote:
Being compatible with C is nice but rather optional ("if possible"). This is getting no where so this will be my last post on this... ALex |
|
|
|
|
|
|
#25 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Your quote is about C++0x, an extension to C++ (just like C99 was an extension of C).
If that compatibility is news to you, i'm sorry, but you should have asked yourself why you could happily (void *) everything, among other things. Last edited by tbp : 01-31-2006 at 08:46 AM. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|