Code :
PORTB = "0b11010000"; Question :
I am now learning to program a 8 bit microcontroller PIC18F8722 using C language on MPLab.
How can I mirror this binary PORTB bits from "11010000" to "00001011"?
The best Article about Bits Operations in C is Bit Twiddling Hacks written by Sean Eron Anderson. He has provided many types Bit functions to do things in optimized way. His methods are accepted worldwide and many library use them to do things in Optimized way. The Way of reverse of Bits provided by him:
unsigned int v; // input bits to be reversed unsigned int r = v; // r will be reversed bits of v; first get LSB of v int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end for (v >>= 1; v; v >>= 1){ r <<= 1; r |= v & 1; s--; } r <<= s; // shift when v's highest bits are zero For other types view the article
This is rendition of an embedded guy:
unsigned char Reverse_bits(unsigned char num){ int i=7; //size of unsigned char -1, on most machine is 8bits unsigned char j=0; unsigned char temp=0; while(i>=0){ temp |= ((num>>j)&1)<< i; i--; j++; } return(temp); } To use it just pass it Port_B where it should be defined in your device header file as something like:
# define Port_B *(unsigned char*)( hard_coded_address_of_portB)
PORTBis probably not a string, and there's no such thing as0b-prefixes in C.