Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Getting Bits out of Bytes

Status
Not open for further replies.

tom_pay

Member
Hi

I am trying to get a PIC to send a byte to another PIC via 433MHz transmitters and receivers.

To get them to talk I need to get the specific bits out of a byte.

How can I do this in a quick and efficient manner?

My current coding is:

HTML:
bit7 = (byte_variable >> 7 ) & 0b1
; ect.


I was hoping to have a sort of thing that could be like this:

Code:
byte_variable [ bit number ] = bit7

That way it could be used in a for loop.

Thanks

Tom
 
hi,
You could rotate thru carry and test the carry bit
 
Ummm, I am only new to PIC's. Could you please explain that a little further. Sorry!!

Thanks

Tom

Hi Tom,
I dont use 'C', but in assembler you would shift the Byte left or right depending which order you want to send the bits; ie ; lsb or msb first.

As the Byte is shifted, test the carry flag for a '1' or '0' and transmit it, do that 8 times for all the bits.
 
This will send LSB first,
Code:
    for(i=0;i<8;i++){
        bit=(byte_var>>i)&1;
        //do something with bit
    }


MSB first is,
Code:
    for(i=7;i<8;i--){
            bit=(byte_var>>i)&1;
            //do something with bit
    }

// a better but less efficient way would be,
    for(i=0;i<8;i++){
        bit=(byte_var>>(7-i))&1;
        //do something with bit
    }
The i<8 in the first example is so i doesn't need to be signed.

Mike.
 
Last edited:
This will send LSB first,
Code:
    for(i=0;i<8;i++){
        bit=(byte_var>>i)&1;
        //do something with bit
    }
One quick tip: If you're using BoostC you can't use "bit" as a variable because it's reserved as a variable type. That one bit me recently. The error message is vague and doesn't point to the actual problem. Just something to keep in mind.
 
Some compliers will let you do a simple
var.7 = 1 or var.7<>1

Look under Alias and Modifiers in your complier help file
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top