Jump to content

Home

Feel like a dufus... Coding question!


babywax

Recommended Posts

LOL what a coincidence... I'm 14 as well :D Started a year ago, though...

 

Some more bitwise operators:

 

The ~ unary operator

--------------------------------

 

Switches bits which are on off, and ones that are off on. I.e.

 

~1 = 0 (pre: 1, post: 0)

~1010 = 0101 (pre: 10, post: 5)

~11011000 = 00100111 (pre: 216 post: 39)

 

When used with & it's very useful - it allows you to separate sections of bits from a bigger number. For example, if you want to take somebody's lightsaber away, then you would do

 

client->ps.stats[sTAT_WEAPONS] &= ~(1 << WP_SABER);

 

stats[sTAT_WEAPONS] is an array with 16 bits, and each bit represents a weapon. If the bit is on then the player has that weapon; if it's off then he/she doesn't have that weapon. ~(1 << WP_SABER) turns off the bit that corresponds with the saber and turns on all the other ones.

 

If you have the saber, then it turns that bit off (1 & 0 = 0) and all the other bits stay the same, therefore the weapons are retained (if the bit is off, it stays the same: 0 & 1 = 0. if it's on, then 1 & 1 = 0 so nothing changes).

 

The left shift operator <<

---------------------------------------

 

The left shift operator simply shifts the number in binary form one bit position to the left. e.g.

 

00000110 << 3 = 00110000 (pre-shift: 6, post-shift: 96)

00000001 << 1 = 00000010 (pre-shift: 1, post-shift: 2)

 

It's the same as multiplying the number by 2 to the power of the number of positions shifted left.

 

The right shift operator >>

-------------------------------------

 

The right shift operator, like the left shift operator, shifts the number in binary form one bit position to the right, e.g.

 

00000110 >> 1 = 00000011 (pre-shift: 6, post-shift: 3)

11110000 >> 4 = 00001111 (pre-shift: 240 post-shift: 15)

 

If you keep shifting and there isn't a position for the bit to move into it simply disappears. So

 

00000110 >> 2 = 00000001 (pre-shift: 6, post-shift: 1)

 

It's the same as as dividing the number by 2 to the power of the number of positions shifted right (except when there are no spaces left).

 

It's useful when you want to get rid of a portion of a number that you don't need. I can't think of a specific example at the top of my head but I'm sure there are some.

 

Bitwise excusive OR ^:

-----------------------------------

 

It's the same as | but if the corresponding bits are both 1, then the result is 0.

 

Logic:

0 ^ 0 = 0

0 ^ 1 = 1

1 ^ 0 = 1

1 ^ 1 = 0

 

Example:

1110 ^ 0011 = 1101

1000 ^ 1001 = 0001

 

Can't think of where you can use this either, but there are probably some situations... otherwise it wouldn't have been added to C, wouldn't it?

 

Hope this helps,

Commodus

Link to comment
Share on other sites

Well, I've been programming for two years, unfortunately the only language my school offers is visual basic.....which is pretty much really crappy.......I rather don't like visual basic. But I have been trying to teach myself C++ for the past couple of weeks, so hopefully I won't have too hard of a time next year in college :D

Link to comment
Share on other sites

Ooops. Sorry about that. I live in Hong Kong in China, so around the time you go online I go to sleep. There was a football match between Man U. and Real Madrid on at 2:30 in the morning so I thought I could go for sleep for 3 hours (this was at 11:00 at night) then get up again at 2, chat on #jk2coding and watch the game and do both at the same time. Only problem was I couldn't be woken up from my sleep. Sorry :(

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...