473,396 Members | 2,154 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,396 software developers and data experts.

Q: Best way to do this?

Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-
unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values[i]);
}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.
Is this a sensible solution?

Thank-you.
Sep 10 '07 #1
4 1227

<sc********@shaggy.towrote in message
news:q0********************************@4ax.com...
Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-
unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values[i]);
}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.
Is this a sensible solution?
It is OK, but not terribly efficient.
You need to construct a variable like 00000111, and then OR, to set the last
three bits.
So
unsigned char mask = 0xFF << decimal;
mask = ~mask;
x |= mask;

will do it quite nicely. On a really simple processor the << decimal will be
a hidden loop, but it should be just one or two instructions on anything
that does any serious processing.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 10 '07 #2
sc********@shaggy.to wrote:
>
Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-

unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values[i]);
}
You can replace (values[i]), with (1U << i) - 1).
I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.
Is this a sensible solution?
Yes.

--
pete
Sep 10 '07 #3
<sc********@shaggy.toa écrit dans le message de news:
q0********************************@4ax.com...
Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-
unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};
if the index into this array is the decimal value mentioned above, you got
it wrong: C arrays are 0 based, so you should make the array at least 8
elements long (0 thru 7) or preferably 9 (0 thru 8 inclusive).
main ()
should be: int main (void)
{
unsigned char i;
better make the loop index an int.
for (i = 0; i < 8; i++) use_byte(values[i]);
OOPS: you access 8 elements from an array of 7 ;-) see above
>
}
should return 0.
I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.
but it cannot hurt to have a generic solution that handles all cases.
Is this a sensible solution?
providedyou fix the array size and contents, it works, but it is neither
efficient nor safe:
- initializing the array with the values of 2 to the n - 1 by hand is error
prone, just imagine if you needed 32 values.
- there is a much simpler solution with the bit shift operator:

the bits you want to set can be computed very efficiently as ((1 << n) - 1)
for values of n between 0 and 2 less than the size of an int, amply
sufficient for your needs.

you then use the bitwise or to set them into the word:

value |= ((1 << n) - 1);

--
Chqrlie.

PS: you can use ((1U << n) - 1) for an unsigned result upto 1 less than the
size of an unsigned int.
Sep 11 '07 #4
On Sep 11, 5:39 am, scooby....@shaggy.to wrote:
Hi,

I need to turn certain bits in a 8-bit word on depending on a decimal
value. For example if the decimal value is 1, turn the first bit on.
Decimal = 5 turn the first 5 bits on, and so on. Now I don't know the
correct terminology for what I'm trying to do, so that hasn't helped
me looking in reference manuals or the Internet. The only way I can
see of doing it is:-

unsigned char values[7] = {1, 3, 7, 15, 31, 63, 127};

main ()
{
unsigned char i;

for (i = 0; i < 8; i++) use_byte(values[i]);

}

I do not need to worry about the last bit as if the byte is full (i.e.
decimal 8) that is handled differently.

Is this a sensible solution?

Thank-you.


yes, it is a good idea to use '|' to make this.
further more, if you want to change a certain bit to opposite, using
xor, '^'.
I hope there's something useful for you.

Sep 11 '07 #5

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

Similar topics

7
by: Gemini | last post by:
Hello I am looking for the best content/article manager software, perferred open source, php, mysql backend.. can anyone recommend me one? I think that the best discussion is phpbb, the...
18
by: Roman Suzi | last post by:
;-) Just type into google "best programming language" and press (I am lucky) Sincerely yours, Roman Suzi -- rnd@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: Irene | last post by:
Hi all, I have set up a simple VB program (and later on an ASP interface) to manage an Athletics database. I'm using Access 2000. To simplify, I have the Athlets, the Competitions and the...
5
by: l.woods | last post by:
I want your recommendation on which ASP.NET Shopping Cart software I should buy? Best code Best documentation Best support (if needed. I will buying source code, if possible) TIA, Larry...
4
by: Ron Brennan | last post by:
Good evening, Windows 2000, JDK 1.5. What opinions do people have on what way and tool programmaticly produces the best quality thumbnails from larger images? On the web I've seen Java...
7
by: Frank Millman | last post by:
Hi all Assume a 2-dimensional list called 'table' - conceptually think of it as rows and columns. Assume I want to create a temporary copy of a row called 'row', allowing me to modify the...
9
by: optimistx | last post by:
Which url in your opinion would be a good or even the best example of javascript usage in a set of pages at least say 10 or more pages? How to use css, how to split js-code to files, how to code...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
9
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha...
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: 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?
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
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.