questions about ">>", "|", '0xff',

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
szerg
Posts: 15
Joined: Mon Jun 05, 2006 4:23 pm

questions about ">>", "|", '0xff',

Post by szerg »

Hi!
I need some help!
so at first... this is about c#... but at least c syntax(it may not differ on e.x. c++..)

here's some code (can be found in Color.cs)[irrnetCP Types]:

Code: Select all

        public int NativeColor
        {
            get
            {
                return ((A & 0xff) << 24) |
                       ((R & 0xff) << 16) |
                       ((G & 0xff) << 8) |
                        (B & 0xff);
            }
            set
            {
                A = (value >> 24) & 0xff;
                R = (value >> 16) & 0xff;
                G = (value >> 8) & 0xff;
                B = (value) & 0xff;
            }
}
when i first looked at that, i had some questions like:
what the *** is that: >>, <<, | and 0xff ???

then i searched a lil'bit and found these:
">>" is and Operator called right shift-ing (or whatever) ..
i read some things about it on msdn, but couldnt really understand(because of my low english/ computer knowledge)
BUT how the * could i get this:

Code: Select all

int i = -1000;
Console.WriteLine(i >> 3);
//...returns:
//-125
0xff and stuff is something in connection with bits.. right?
..like i have a number and i can write in this 0x*****... ?

| is of course logical.. but when im dealing with bits is called BITWISE logic... and as i could understand: it's like mergeing( :shock: :roll: ) 2 bits..

so at the end i could find this out:
when i call this:

Code: Select all

                return ((A & 0xff) << 24) |
                       ((R & 0xff) << 16) |
                       ((G & 0xff) << 8) |
                        (B & 0xff);
i kinda split the A,R,G,B (0-255)[1 byte = 8 bit] values into one 32 bit [4 x 1 byte] value.. from specified bit numbers: 0 > 8 > 16 > 24

SO... could you answer my last questions:
  • aim i right? :lol:
    how can i translate an int to 0x*****?
    make me understand using shifting!
Thank You! :wink:
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

0xff is a hexadecimal number which is base 16. Binary is base 2 and decimal is base 10 so that might give you an idea about how it works but it's a complete nightmare really if you've not come across it before. Basically each digit has a value from 0-15, the usual digits 0-9 and then A-F as well to allow you to represent double digit numbers with only one digit.. sort of thing... So 0xA is 10 in decmal and 0xB is 11 is decimal etc. (the 0x bit just shows you it's in hex).

And what the code in Color.cs you posted is doing is storing a colour value in an integer variable and doing bitwise shifts to get the individual colour values in and out of it.

The bitwise operators are similar to the logical operators. Like && (in C) is logical AND and it compares two boolean values and returns TRUE if both of them are TRUE and FALSE otherwise. With the bitwise operator & it acts on bits and returns 1 if both of the bits are 1 and 0 otherwise. Similarly for the | bitwise operator, it returns 1 if either of the bits are 1 and 0 if both of them are 0.

Hope that explains some of your questions, if you google any of this stuff you might get better explanations!
Image Image Image
szerg
Posts: 15
Joined: Mon Jun 05, 2006 4:23 pm

Post by szerg »

a year ago or so there was something about conevrting decimal numbers to binary on "computer lesson" in school but i didnt really need it so far..
and..thx for your reply! it made me see a lot of things through

so i asked coz i needed the best way to send Vectors / matrices' data through udp.. and i was thinking.. putting them into one string with separators is not allright.. then i remembered that i saw this NativeColor thing in Color.cs.. so i tought what about putting'em into one number...?
but i discovered, it is not all right cause: 1 vectors' coordiates are floats, and could be a bigger number than f.ex 255
szerg
Posts: 15
Joined: Mon Jun 05, 2006 4:23 pm

Post by szerg »

.. when im transfering f.ex a Vector3D data.. is it useful to transfer its Legth and LengthSQ?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I guess if you're gonna use those values a lot then it would be a good idea to send them (if that's efficient to send extra values) as it would save having to recalculate them a lot.

presumably this is a server app you're working on, so if the server was working for lots of machines it could save time for each machine if it had the values sent to it, but then maybe it would slow down the sever a bit by doing the calculations there.... hard to say!
Image Image Image
szerg
Posts: 15
Joined: Mon Jun 05, 2006 4:23 pm

Post by szerg »

but these values are not so hard to calculate, and would take more cpu time/bandwidth from the server to send it to each client.

so im using these sending/recieving methods ... not a big thing but works..
http://irrlichtnetcp.sourceforge.net/ph ... .php?t=837
Post Reply