Author Topic: Bit operation question  (Read 1654 times)

0 Members and 1 Guest are viewing this topic.

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Bit operation question
« 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.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Bit operation question
« Reply #1 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

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Bit operation question
« Reply #2 on: July 01, 2010, 12:57:08 PM »
Thanks!

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

Like num<<=1 ?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Bit operation question
« Reply #3 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

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Bit operation question
« Reply #4 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

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Bit operation question
« Reply #5 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
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Bit operation question
« Reply #6 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
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

 


data_list