471,092 Members | 1,028 Online

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

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

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

Thanks,
Doubty

Aug 31 '06 #1
7 4071 fa***********@gmail.com wrote On 08/31/06 15:03,:
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
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);

--
Er*********@sun.com

Aug 31 '06 #2
fa***********@gmail.com wrote:
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?

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 31 '06 #3
fa***********@gmail.com writes:
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
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;

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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Aug 31 '06 #4
fa***********@gmail.com schrieb:
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
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
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

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 31 '06 #5
Michael Mair wrote:
fa***********@gmail.com schrieb:
>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
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
11110010
or
11111110
The rule as stated will give the latter. I don't see any ambiguity.

--
Sep 1 '06 #6
fa***********@gmail.com wrote:
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

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);

--
Peter

Sep 1 '06 #7
Michael Mair wrote:
>fa***********@gmail.com schrieb:
>>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

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
11110010
or
11111110

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"
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 1 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 19 posts views Thread by Lauren Quantrell | last post: by 1 post views Thread by Logan X via .NET 247 | last post: by 4 posts views Thread by Eric Lilja | last post: by 7 posts views Thread by whatluo | last post: by 3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by 7 posts views Thread by patang | last post: by 6 posts views Thread by patang | last post: by 1 post views Thread by johnlim20088 | last post: by reply views Thread by Debadatta Mishra | last post: by reply views Thread by MartianBanks | last post: by reply views Thread by clicknium | last post: by reply views Thread by autodeveloper | last post: by reply views Thread by vishwasr | last post: by 3 posts views Thread by OSWOM | last post: by 1 post views Thread by Chris3020 | last post: by reply views Thread by jmwhawk | last post: by reply views Thread by Synco | last post: by 6 posts views Thread by jithb4u | last post: by