473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
should be made 1.

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

Thanks,
Doubty

Aug 31 '06 #1
7 4257


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

--
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
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.
--
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
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
--
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
should be made 1.
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.

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

--
Peter

Sep 1 '06 #7
Thad Smith schrieb:
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
should be made 1.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.