Whats this symbol?

Started by
10 comments, last by nprz 18 years, 3 months ago
i got a good tutorial on smooth scrolling and the writer used this map_xoff = map_drawx & 15; whats the AND symbol mean? and if its not c++.. what symbol would i use for c++?
Advertisement
It's the bitwise and.

Link

If I'm wrong, don't shoot me. It's hard to tell what the & is trying to do since I have no idea what the rest of the code is doing. But I'm going to go along and assume its the bitwise AND operator in c++. Here is a thing straight from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_bitwise_and_operator.asp

C++ Language Reference    Bitwise AND Operator: &Grammar and-expression: equality-expressionand-expression & equality-expression The bitwise AND operator (&) compares each bit of the first operand to thecorresponding bit of the second operand. If both bits are 1, the correspondingresult bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise AND operator must be of integral types. The usualarithmetic conversions covered in Arithmetic Conversions, are applied to theoperands. Operator Keyword for & The bitand operator is the text equivalent of &. There are two ways to accessthe bitand operator in your programs: include the header file iso646.h, orcompile with the /Za (Disable language extensions) compiler option. Example// expre_Bitwise_AND_Operator.cpp// compile with: /EHsc// Demonstrate bitwise AND#include <iostream>using namespace std;int main() {   unsigned short a = 0xFFFF;      // pattern 1111 ...   unsigned short b = 0xAAAA;      // pattern 1010 ...    cout  << hex << ( a & b ) << endl;   // prints "aaaa", pattern 1010 ...}


EDIT: shoot you barely beat me to it
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
sheesh bits and stuff... i never got to that kinda thing.. looks kinda crazy lol but thanks.. ide rate you but i wont do much help.
In that context, you should/could also write
map_xoff = map_drawx % 16;

This would be more consistant with tiling because you have
const int TILE_SIZE = 16;
mapx = map_drawx / TILE_SIZE;
map_xoff = map_drawx % TILE_SIZE;


% would be the modulos operator (like a remainder in math).
im sorry for the horrible english.

wait there cool things with these operators they let you set flags with ORing(if im not mistaken) the ORing works like this say if you had some properties that the option of beeing on and off well normal you would have some boolean varible but if you have a group of these you can use ORing to set the property
Like sdl's Init SDL_Init() or better yet window's CreateWindow methods.They can be used for other things in 2d graphics include color keying.
Bring more Pain
void DrawTiles(){short i,j;short MapX, MapY;short XOff, YOff;MapX = MapDrawX/32;MapY = MapDrawY/32;XOff = MapDrawX & 31;YOff = MapDrawY & 31;for(i = 0; i &lt; 16; i++){    for(j = 0; i &lt; 21; j++)    {        BlitTile(j * 32 - XOff, i * 32 - YOff, MapX,MapY);    }}}//end of Draw Tilesvoid BlitTile(int x, int y, int mapC, int mapR){    SDL_Rect Ofs;    Ofs.x = x;    Ofs.y = y;    switch(Map[mapR][mapC])    {        case 0:        SDL_BlitSurface(Grass, NULL, MainScreen, &Ofs);break;        case 1:        SDL_BlitSurface(Rock, NULL,MainScreen, &Ofs);break;    }}//end of blittile

thats my code sorry its short so i didnt htink i needed a source thingy... anyways... with this code my screen stays black...and i have to use Ctrl + Alt + Delete in order to get out of it... i know ive been a hassle latley with these questions but im so close! lol thanks alot guy for your time
hmm okay i think i fixed it.... thanks anyways!
Quote:Original post by nprz
In that context, you should/could also write
map_xoff = map_drawx % 16;


That would be a deliciously bad idea for performance on embedded systems, where the compilers are generally not smart enough to perform the optimization and operator % takes a long time to compute. In any case, however, a clean comment explaining the operation would be more than welcome.

Quote:Original post by ToohrVyk
Quote:Original post by nprz
In that context, you should/could also write
map_xoff = map_drawx % 16;


That would be a deliciously bad idea for performance on embedded systems, where the compilers are generally not smart enough to perform the optimization and operator % takes a long time to compute. In any case, however, a clean comment explaining the operation would be more than welcome.


I guess so.
map_xoff = map_drawx & (TILE_SIZE - 1);
would probably be faster, right?

Oh, is
x % y == x & y - 1 (if x & y > 0)?
if so, wouldn't compilers be smart enough to make that optimization?

[Edited by - nprz on January 5, 2006 4:42:21 PM]

This topic is closed to new replies.

Advertisement