To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 24371
24370  |  24372
Subject: 
Rotating through a four-bit nibble
Newsgroups: 
lugnet.robotics
Date: 
Tue, 20 Sep 2005 15:58:18 GMT
Viewed: 
1172 times
  
   No, it's not pornographic (look over in rtlToronto - I hear it's a fun
thread). But I'm working in NQC, and I need to rotate the bit series in a 4-bit
(or 8-bit) nibble embedded in a 16-bit word. Annoying. Any suggestions? Masking
the desired four bits out of a storage variable and into a temporary variable
four times (so it's a repeating pattern) and then rotating and masking out the
desired sequence might work, but is there a better way? Bit-level manipulation
is not my strong suite.

(x's here are "something else" that I may wish to keep intact, y is garbage or
potentially trashed bits)

stored sequence in 16-bit word:        xxxx xxxx 0110 xxxx
4-bit nibble copied across temporary:  xxxx 0110 0110 0110
left shift temporary, say, twice:      xx01 1001 1001 10yy
mask out one nibble back into storage: xxxx xxxx 1001 xxxx

There's got to be a better way. I'm using NQC, so I'm stuck with those big
16-bit words and basic operations like bit logic, shifts, and interger division
(and of course modulo division).

   Anybody?

--
Brian Davis



Message has 3 Replies:
  Re: Rotating through a four-bit nibble
 
(...) One way to do this might be by using multiplication and division. Temp = (Input & 0000 0000 0011 0000)*4 + (Input & 0000 0000 1100 0000)/4 Output = (Input & 1111 1111 0000 1111) & (Temp & 0000 0000 1111 0000) Although I imagine that can be (...) (19 years ago, 20-Sep-05, to lugnet.robotics)
  Re: Rotating through a four-bit nibble
 
(...) What are you optimizing for? Code clarity? Code speed? Code length? There are (to my knowledge) no truly pretty solutions to this. I would tend to go with: int rotbits(int input, int numbits, int offset, int rotdist) { int mask = (~((~0) << (...) (19 years ago, 20-Sep-05, to lugnet.robotics)
  Re: Rotating through a four-bit nibble
 
(...) Well, your example has the nibble of interest in the middle of the register. Due to boundary conditions, it might be cheaper to have it in the least significant bits of the 16 bit register. In your example you are rotating by an aribtrary (...) (19 years ago, 21-Sep-05, to lugnet.robotics)

13 Messages in This Thread:




Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR