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 / 24373
24372  |  24374
Subject: 
Re: Rotating through a four-bit nibble
Newsgroups: 
lugnet.robotics
Date: 
Tue, 20 Sep 2005 17:45:52 GMT
Original-From: 
T. Alexander Popiel <popiel@%IHateSpam%wolfskeep.com>
Viewed: 
1105 times
  
In message:  <In4Hp7.qGD@lugnet.com>
             "Brian Davis" <brdavis@iusb.edu> writes:
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?

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) << numbits)) << offset;
    int left = rotdist % numbits;
    int right = (numbits - left) % numbits;
    int temp = input & mask;
    return (input & ~mask) | (((temp << left) | (temp >> right)) & mask);
  }

This generic solution is (of course) slower (and a bit harder to read)
than a solution specifically tuned for a particular size, offset, and
rotation distance... but it's 7 lines of code that can then be called
wherever you need to do these sub-word manipulations.  Depending on
how good the NQC optimizer has gotten, it might even simplify down to
the faster versions when given constants for the size and offset...

- Alex



Message is in Reply To:
  Rotating through a four-bit nibble
 
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 (...) (19 years ago, 20-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