How do you assign a bit from a byte to a bit variable?

J

johannblake

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;
}

I don't believe this will work because there is no indication what is
really being assigned to bitVariable. My compiler doesn't complain and
I find that strange. Essentially, I need to shift the bits in "data" to
an output port on my microcontroller and want to do it as quickly as
possible.
 
I

Ian Collins

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;

Do you want the data to be in the MS bit?

Should it be

bitVariable = (data >> i) & 0x01;
 
J

johannblake

When shifting the data out of the 'data' variable, the bit in the most
significant bit position must be assigned to the variable
'bitVariable'.
 
I

Ian Collins

When shifting the data out of the 'data' variable, the bit in the most
significant bit position must be assigned to the variable
'bitVariable'.
Please don't top post.

So bitVariable is a hardware register?
 
J

johannblake

I thought of that as well and am currently doing it that way. But is it
the most efficient way? An in-line comparison will take up an extra
clock cycle.

By the way, I think your example is incorrect. I believe it should be:

bitVariable = (data << i) & 0x80 ? 0x80 : 0;
 
J

johannblake

bitVariable is actually a bit-field in a structure. It refers to a
single output port that consists of 8 outputs.
 
I

Ian Collins

bitVariable is actually a bit-field in a structure. It refers to a
single output port that consists of 8 outputs.
Please quote some context so your reply makes sense.

Your method will work, provided you do something with the value each
time round the loop.
 
M

Mark Laczynski

I thought of that as well and am currently doing it that way. But is it
the most efficient way? An in-line comparison will take up an extra
clock cycle.

By the way, I think your example is incorrect. I believe it should be:

bitVariable = (data << i) & 0x80 ? 0x80 : 0;

I don't think it matters between

bitVariable = (data << i) & 0x80 ? 0x80 : 0;

and
bitVariable = (data << i) & 0x80 ? 1 : 0;

because if bitVariable is only 1 bit... then why assign a value greater
than 1?
And I know my version works cause I compiled it and ran it myself, but
your way may work also... try it out

Mark
 
C

CBFalconer

Sorry, I screwed up. Your code example is correct.

Include context. Your articles are meaningless without it. For
the technique of so doing on the broken google interface to usenet,
see my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
P

pete

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;
}

I don't believe this will work because there is no indication what is
really being assigned to bitVariable. My compiler doesn't complain and
I find that strange.
Essentially, I need to shift the bits in "data" to
an output port on my microcontroller and want to do it as quickly as
possible.

#include <limits.h>

for (i = 0; i < CHAR_BIT; i++) {
bitVariable = data >> CHAR_BIT - 1 - i & 1;
}
 
P

pete

pete said:
(e-mail address removed) wrote:

#include <limits.h>

for (i = 0; i < CHAR_BIT; i++) {
bitVariable = data >> CHAR_BIT - 1 - i & 1;
}


i = CHAR_BIT; /* or i = 8; I suppose */
while (i-- != 0) {
bitVariable = data >> i & 1;
}
 
R

Roberto Waltman

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;
}

I don't believe this will work because there is no indication what is
really being assigned to bitVariable. My compiler doesn't complain and
I find that strange. Essentially, I need to shift the bits in "data" to
an output port on my microcontroller and want to do it as quickly as
possible.

Mark is right, you should use something similar to

bitVariable = (data << i) & 0x80 ? 1 : 0;

See the following:

---------------------------------------
[rwaltman tmp]$ cat bit_test.c

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct xx { unsigned int bit : 1; } yy ;
yy.bit = 0x80;
printf("bit=%d\n", yy.bit);
yy.bit = 0x01;
printf("bit=%d\n", yy.bit);
return EXIT_SUCCESS;
}
[rwaltman tmp]$ gcc -ansi -Wall -pedantic -W -o bit_test bit_test.c
[rwaltman tmp]$ ./bit_test
bit=0
bit=1
[rwaltman tmp]$
 
M

Martin Ambuhl

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;
}

I don't believe this will work because there is no indication what is
really being assigned to bitVariable. My compiler doesn't complain and
I find that strange. Essentially, I need to shift the bits in "data" to
an output port on my microcontroller and want to do it as quickly as
possible.

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned char bit, byte = 0x73, mask = 1u << (CHAR_BIT - 1);
printf("The original byte is %#04o (%#04x)\n"
"results of shifting follow:\n", byte, byte);

while (byte) {
bit = !!(byte & mask);
byte <<= 1;
printf("bit: %o, remaining byte: %#04o (%#04x)\n", bit, byte,
byte);
}
return 0;
}


The original byte is 0163 (0x73)
results of shifting follow:
bit: 0, remaining byte: 0346 (0xe6)
bit: 1, remaining byte: 0314 (0xcc)
bit: 1, remaining byte: 0230 (0x98)
bit: 1, remaining byte: 0060 (0x30)
bit: 0, remaining byte: 0140 (0x60)
bit: 0, remaining byte: 0300 (0xc0)
bit: 1, remaining byte: 0200 (0x80)
bit: 1, remaining byte: 0000 (0000)
 
R

Rod Pemberton

Mark said:
What I would do is

bitVariable = (data << i) & 0x80 ? 1 : 0;

I thought of that as well and am currently doing it that way. But is it
the most efficient way? An in-line comparison will take up an extra
clock cycle.

A shift probably takes longer, but it's worth a try. Depending on the CPU,
Mark's version will probably require a comparison and a branch. For one
common CPU, shifts of four or less are faster than a comparison and a branch
even with good branch prediction by the CPU. This could result in slightly
faster code.

bitVariable = ((data << i) & 0x80) >>7);

A few people here will scream since you've used non-portable code. You've
assumed CHAR_BIT is 8.


Rod Pemberton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top