473,804 Members | 3,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert 00010000 to 11110000......h ow?

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 4315


fa***********@g mail.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***********@g mail.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)gma il.com | don't, I need to know. Flames welcome.
Aug 31 '06 #3
fa***********@g mail.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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Aug 31 '06 #4
fa***********@g mail.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(B yte) != 0)
{
Dest = FillWithOnesFor UnsetHigherValu eBits(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 = FillAllBitsOneW ithZerosForUnse tLowerValueBits (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***********@g mail.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***********@g mail.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***********@g mail.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 "specificat ion"
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
7294
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. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
1
1787
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 would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
4
3639
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
7
7128
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 without strtol or s/printf function. Thanks, whatluo.
3
10296
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
7
29249
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
1407
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 declaration statments (first lines) e.g. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String Private Function ConvertHundreds(ByVal MyNumber As String) As String etc.
1
3604
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 some files and work on it. Let say, I want add new page to web project named websiteOrder.sln, i will open websiteOrder.sln in my local computer, connected to websiteOrder.sln located in Visual Source Safe 6.0(source safe located in another...
0
10790
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.