Public Group

The & Operator

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

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 on other sites
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 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 on other sites
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 on other sites
Hmmm... that was me above, somehow I got logged out.

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.

        00000101 (5)      & 00011111 (31)      ===============        00000101 (5)

Share on other sites
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 on other sites
He's using & to implement a mod as a (theoretical) optimization over using %, the modulus operator.

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 on other sites
Quote:
 Original post by LessBreadANDing 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'

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634111
• Total Posts
3015575
×