Jump to content
  • Advertisement
Sign in to follow this  
HolySheepy

The & Operator

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have some problems understanding how & works with decimal or float numbers lets say
#include <iostream.h>
int main()
{
	double floating=5&31;
	cout << floating << endl;
	return 0;
}

Gives me 5 but why? Windows Calculator Decimal Mode -> 5 -> Binary Mode -> 101 Decimal Mode -> 31 -> Binary Mode -> 11111 so how the & uses them? or how he makes them same length in order to use them?

Share this post


Link to post
Share on other sites
Advertisement
Not knowing the inner workings of the CPU, I'd say it does something like this (well I'd say Windows Calculator does something like this):

 5 = 00000101
31 = 00011111 &
 5 = 00000101

Share this post


Link to post
Share on other sites
That's not actually doing stuff to floating point numbers. You have two integers there, so you're doing an integer op. Then you're converting the integer result to a double, and storing the resultant double.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That's the right answer. Even though Win. calculator truncates the leading 0's from those binary numbers, they're actually:

5 = 00000101
31 = 00001111

(they're actually a lot longer as a double is actually 64 bits (not 8 as I used above)).

Anyway, then you can allign the bits and see that 5 & 31 is indeed 5.

Share this post


Link to post
Share on other sites
& (and):
    0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
| (or):
    0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
^ (exclusive or):
    0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
&, |, and ^ are "bit-wise" operators meaning that they operate on each bit of the operands individually.

So in your case,
        00000101 (5)
& 00011111 (31)
===============
00000101 (5)

Share this post


Link to post
Share on other sites
thanks for your answers
but i still dont understand why it is useful there:

for (y = 0; y < SCREEN_SIZEY; y++)
{
for (x = 0; x < SCREEN_SIZEX; x++)
{
// (NEW)
scroll_x = (world_camerax / TILE_SIZE);
scroll_y = (world_cameray / TILE_SIZE);

// (NEW)
offset_x = scroll_x & (TILE_SIZE - 1);
offset_y = scroll_y & (TILE_SIZE - 1);

tile = map[scroll_y][scroll_x];

http://www.gamedev.net/reference/articles/article1242.asp
i mean whats the good thing in &'ing this?

Share this post


Link to post
Share on other sites
He's using & to implement a mod as a (theoretical) optimization over using %, the modulus operator.

Share this post


Link to post
Share on other sites
ANDing works best with integers. AND floats/doubles you'll lose everything right of the decimal - so it kind of blows the point of using reals.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
ANDing works best with integers. AND floats/doubles you'll lose everything right of the decimal - so it kind of blows the point of using reals.


You can't use bit-wise operators on floats and doubles.
int main(int argc, char* argv[])
{

std::cout << ( 31.0f & 5.0f ) << std::endl;
return 0;
}

c:\Projects\test_floatand\test_floatand.cpp(9) : error C2296: '&' : illegal, left operand has type 'float'
c:\Projects\test_floatand\test_floatand.cpp(9) : error C2297: '&' : illegal, right operand has type 'float'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!