Society of Robots - Robot Forum

Software => Software => Topic started by: Razor Concepts on July 01, 2010, 08:41:45 AM

Title: Bit operation question
Post by: Razor Concepts on July 01, 2010, 08:41:45 AM
Original operation:
Code: [Select]
((0xFF0000 >> 16) >> 3) << 11
It is NOT equivalent to:
Code: [Select]
0xFF0000 >> 8
It is equivalent to, and the most simplified operation is:
Code: [Select]
(0xFF0000 >> 19) << 11
Is this all correct? Just making sure.
Title: Re: Bit operation question
Post by: waltr on July 01, 2010, 09:31:49 AM
Sounds right.
To check do each shift operation independently and look at the result in your debugger/simulator or on paper.
My best explanation is that once a bit is shifted out of the word (24 bits) the bit is sent to the bit-bucket. When a bit is shifted in from the other end it is a zero shifted in. So breaking the operations down:

0xff0000 >> 16 = 0x0000ff
0x0000ff >> 3 = 0x00001f
0x00001f << 11 = 0x00f800

Whereas:
0xff0000 >> 8 = 0x00ff00

and:
0xff0000 >> 19 = 0x00001f
0x00001f << 11 = 0x00f800

Another way to get this result is:
(0xff0000 >> 8) & 0x00f800
if the 5 bits you need are not always ones
Title: Re: Bit operation question
Post by: Razor Concepts on July 01, 2010, 12:57:08 PM
Thanks!

by the way, what would the "<<=" operator mean?

Like num<<=1 ?
Title: Re: Bit operation question
Post by: waltr on July 01, 2010, 01:54:22 PM
Quote
num<<=1
I wasn't sure so I put that in some code and ran it under the simulator.

It takes the value in num, shifts it left one bit then assigns it back to num. Which makes perfect sense.

If num = 0x05
then do
num<<=1
num is now 0x0A

Does your C compiler have a debugger/Simulator? Its very handy the test what stuf like that does.

Or are you just testing me? lol
Title: Re: Bit operation question
Post by: Razor Concepts on July 01, 2010, 02:05:34 PM
Thanks! my compiler does have a debugger but I just got started using it and MSP430s a few days ago so I am still fairly lost  :D

with so many different commands that can be used... maybe the creators of C put in a special one that unlocked the secrets of the universe  :-X
Title: Re: Bit operation question
Post by: Webbot on July 01, 2010, 09:35:52 PM

by the way, what would the "<<=" operator mean?

Like num<<=1 ?

Its the same as: num = num<<1

In C you often see operators on the left of the equals ie
num *= 10  is the same as num = num *10
num += 10 is the same as num = num + 10
num -= 10 is the same as num = num - 10
etc
Title: Re: Bit operation question
Post by: Soeren on July 03, 2010, 07:33:09 AM
maybe the creators of C put in a special one that unlocked the secrets of the universe  :-X
They did, it's 0x000015 <<=
;D