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. 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 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
<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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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: 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...
| |