Convert 00010000 to 11110000......how?

F

farnaz.shahed

Hi,

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

I tried using left shift n right shift operators but that didnt
help...any other suggesstions pls?

Thanks,
Doubty
 
B

Ben Pfaff

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

If exactly one bit in x is set, and x is an unsigned int or
unsigned long, then
x = ~(x - 1);
should have that effect. But that'll set all the bits in x at or
to the left of the bit in question. If you only want that effect
for the low 8 bits, then you can do
x = (x - 1) ^ 0xff;
instead.

If more than one bit in x might be set, then I think the
following will work:
~((x ^ (x - 1)) >> 1)

I haven't tested any of this. They might not work. If they do,
there might be easier ways to do the same thing.
 
E

Eric Sosman

Hi,

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

Assuming an eight-bit byte:

unsigned char byte = ...;
byte |= (byte << 1) | (byte << 2) | (byte << 3)
| (byte << 4) | (byte << 5) | (byte << 6)
| (byte << 7);

Generalizing to bytes of arbitrary width:

#include <limits.h>
...
unsigned char byte = ...;
int s;
for (s = 1; s < CHAR_BIT; ++s)
byte |= byte << s;

Sneakier method:

unsigned char byte = ...;
if (byte > 0) /* delete if 00...0 should give 11...1 */
byte = ~(((byte & (byte - 1u)) ^ byte) - 1u);
 
C

Christopher Benson-Manica

I tried using left shift n right shift operators but that didnt
help...any other suggesstions pls?

Yes - use the shift operators correctly. Why don't you post your code
and show us that you actually tried the problem?
 
M

Michael Mair

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

Notes:
- bit-fields are something different in C.
- A byte can have more than 8 bits in C; in fact, it has CHAR_BIT
bits.

The above can be described as you did:
unsigned char Byte;
....
if (NumberOfBits(Byte) != 0)
{
Dest = FillWithOnesForUnsetHigherValueBits(Byte);
}
However, this leaves unspecified what happens if you have
two set bits:
00010010
Will this lead to
11110010
or
11111110
If the former is the case, the above is a good description.
If the latter is the case,
Dest = FillAllBitsOneWithZerosForUnsetLowerValueBits(Byte) ;
may be a better description.

I tried using left shift n right shift operators but that didnt
help...any other suggesstions pls?

You can "set" bits with |=, you can "toggle" bits with ^=, you
can test bits with &, you can clear bits with &=, you can shift
bits with <</>>.
You can do this in many different ways.
If I were you, I'd start with writing a function to output unsigned
char values in binary representation, so you can see how your
operations affect the byte.
You will have to apply some of the bit operations so you can
display the byte; then, you can start with modifying the byte.

If you have trouble, copy&paste the _compilable_ code you have
and post it here. Explain what you expect and how your programme
did fall short.


Cheers
Michael
 
P

Peter Nilsson

This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.

I tried using left shift n right shift operators but that didnt
help...any other suggesstions pls?

unsigned char byte = 0x80;
unsigned x = byte;
unsigned char desired = -(x & -x);
 
M

Michael Mair

Thad said:
The terminology above is backwards. If any bit is 1, all bits to the
left of it should be made 1. That means that 00010000 is converted to
11110000. A single example does not imply the rule.


The rule as stated will give the latter. I don't see any ambiguity.

As the terminology was off, I was not sure whether the OP said
what he or she meant and vice versa. Experience with past
discussions (message 2xx in the thread, everything is near
flame war, OP says, "Er, that is not what I meant in the first
place"... ;-)) lead me to caution w.r.t. the "specification"
of the task.


Cheers
Michael
 
T

Thad Smith

The terminology above is backwards. If any bit is 1, all bits to the
left of it should be made 1. That means that 00010000 is converted to
11110000. A single example does not imply the rule.
However, this leaves unspecified what happens if you have
two set bits:
00010010
Will this lead to
11110010
or
11111110

The rule as stated will give the latter. I don't see any ambiguity.
 

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
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top