A simpler expression?

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
((PINA & (1<<3))>0)

I've read through the tutorial in the forum about bitwise operations but I'm still a little confused. I know what the above code does. It gets the value (1 or 0) of the third pin in PORTA. I know that the comparison is redundant for a conditional statment (Any value other than 0 is true) but that isn't why I wrote that code. Is this the most efficient way of getting the value of the PIN or is there another way? Thank you for your advice.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure why you feel the need for the >0 comparison in there? Are you just using this in an if() statement:

if ((PINA & (1<<3))>0) {

in that case:

if (PINA & (1<<3)) {

works as well on the basis that 0 is false. Sure, when the bit is set PINA & (1<<3) will have the numerical value 8 in fact - but that isn't 0 so it's considered true.

If however you were using:

x = ((PINA & (1<<3))>0);

where you wanted x to hold just 0 or 1 then

x = (PINA & (1<<3));

would not work, it'd be 0 or 8. But you could use:

x = (PINA & (1<<3)) ? 1 : 0;

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Not sure why you feel the need for the >0 comparison in there? Are you just using this in an if() statement:

Essentially, this is my first guess at trying to flip an eight bit number which just so happens the signals coming in from PORTA. Logically, I should have wired it the right way but it was essentially a compromise between simple code or neater routing. I choose neater routing while also forgetting that bitwise shifting does not wrap around.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You mean you are trying to flip 11011000 so it becomes 00011011 ? This has come up MANY MANY times here and the algorithms for bit flipping are well known. Doing multiple if()'s is not the "best" solution. I like the one that involves the 55's and AA's myself.

Wonder what "flip byte" yields in search?

Cliff

EDIT: Just one of the many threads:

https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
You mean you are trying to flip 11011000 so it becomes 00011011 ? This has come up MANY MANY times here and the algorithms for bit flipping are well known. Doing multiple if()'s is not the "best" solution. I like the one that involves the 55's and AA's myself.

Why do you keep on assuming that I was using multiple if()'s? I'm wrote more inept code than that.:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry but given the limited amount of info in your post I ASSUMED (incorrectly it seems) that your:

((PINA & (1<<3))>0)

was being used in a logical expression. Presumably it's actually in an assignment then?

Anyway, I've given you a link to an overall (good) solution to the bit flipping thing - your choice whether you use it or ignore it I guess.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Sorry but given the limited amount of info in your post I ASSUMED (incorrectly it seems) that your:

((PINA & (1<<3))>0)

was being used in a logical expression. Presumably it's actually in an assignment then?

Anyway, I've given you a link to an overall (good) solution to the bit flipping thing - your choice whether you use it or ignore it I guess.


I just realized the idiocy of what I was doing. In my process of creating a bitmask I didn't realize that five steps later I created the same bitmask. It was one of those days yesterday. I asked the lady at the store if she had any cat stuffed animals and she walks up and points to the tiger right in front of me.

((PIN & (1<<PINNUMBER)>0)>>PROPERPOSITION)
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You did read that link I gave didn't you? - there are FAR easier ways to flip bits given there.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
You did read that link I gave didn't you? - there are FAR easier ways to flip bits given there.

I did. That code was what I was doing before. Im working on the simpler solution right now.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if you have plenty of room to waste, and you need to do many bit-flippings, you could use a table...
char mirror[256] = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ... };

conv = mirror[etyb];

-That is simple, it's fast, but it... wastes space.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Im working on the simpler solution right now.
There are lots of simple solutions in the thread Cliff referred to.

A little modification to one in that thread (assuming only needed for porta)-

uint8_t get_porta(void){
    uint8_t temp=PINA;
    temp=((temp>>1)&0x55)|((temp<<1)&0xaa);
    temp=((temp>>2)&0x33)|((temp<<2)&0xcc);
    asm volatile("swap %0":"=r"(temp):"0"(temp)); 
    return temp;
}
//18 words
//usage- my_var=get_porta();

or if those pin mappings are not actually reversed, but are 'mixed up'

uint8_t get_porta(void){
    uint8_t temp=PINA;
    uint8_t retval=0;
    //actual pin here       'virtual' pin here
    if(temp&(1<<PIN0)) retval |= (1<<PIN5);
    if(temp&(1<<PIN1)) retval |= (1<<PIN3);
    if(temp&(1<<PIN2)) retval |= (1<<PIN1);
    if(temp&(1<<PIN3)) retval |= (1<<PIN0);
    if(temp&(1<<PIN4)) retval |= (1<<PIN7);
    if(temp&(1<<PIN5)) retval |= (1<<PIN2);
    if(temp&(1<<PIN6)) retval |= (1<<PIN4);
    if(temp&(1<<PIN7)) retval |= (1<<PIN6);
    return retval;
}
//22 words