#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?
The & Operator
Hi,
i have some problems understanding how & works with decimal or float numbers
lets say
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
5 = 00000101
31 = 00011111 &
5 = 00000101
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.
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.
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.
& (and):
So in your case,
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)
thanks for your answers
but i still dont understand why it is useful there:
http://www.gamedev.net/reference/articles/article1242.asp
i mean whats the good thing in &'ing this?
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?
He's using & to implement a mod as a (theoretical) optimization over using %, the modulus operator.
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.
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'
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement