Public Group

# 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.

## 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'

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011159
• ### Who's Online (See full list)

There are no registered users currently online

×