Anyone know C/RIMS?

Chris0nllyn

Well-Known Member
I've got an 8 bit input ("A") and an 8 bit output ("B"). I'm trying to set B's upper nibble to A's lower nibble and B's lower nibble to 6.

Obviously 6 is just 0x6, but I'm having trouble getting A's lower nibble to line up with B's upper nibble.

code:

#include "RIMS.h"

int main() {
while(1) {
B=A|0x6;
}
return 0;
}
 

Starman

New Member
I didn't just google it for you, but it's from this reference here: http://graphics.stanford.edu/~seander/bithacks.html which every C programer should have bookmarked, and I reference often. I like C, it's still a good language.

You swap nibbles like this:

// swap nibbles ...
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);


It doesn't look to me like what you have now will compile.

http://c-faq.com is another site to keep in your bookmarks.

What kind of micro controllers you workin' on?
 

Chris0nllyn

Well-Known Member
I didn't just google it for you, but it's from this reference here: http://graphics.stanford.edu/~seander/bithacks.html which every C programer should have bookmarked, and I reference often. I like C, it's still a good language.

You swap nibbles like this:

// swap nibbles ...
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);


It doesn't look to me like what you have now will compile.

http://c-faq.com is another site to keep in your bookmarks.

What kind of micro controllers you workin' on?

That certainly helped. Thank you!

The code I ended up using and compiling succesfully is:
#include "RIMS.h"

int main() {
while(1) {
B=0x4|A<<4;
}
return 0;
}

That set B's upper nibble to A's lower and B's lower nibble to 4.


I'm working with an Arduino.
 
Top