473,396 Members | 1,892 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.

Translate but can not C

Hi group!
I'm translating a prog written in IAR C for AVR.
But my knowledge in C is too low to understand this.

//================================================== ========================
===
// Sets or clears the bit 'index' in the bit table 'table', regarding the
value
// of 'value'.

void putBit(unsigned char *table, unsigned char index, unsigned char value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?
tia/ Henning
Nov 14 '05 #1
6 1205

"Henning" <co***********@coldmail.com> wrote

void putBit(unsigned char *table, unsigned char index, unsigned char value) {
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?

*p - take the memory location pointed to by p
= - write to it
(value) ? x : y - if value is non-zero write x, else write y
m | *p - logical OR of m and *p
~m & *p - logical AND of complement of m and *p

the ? : syntax is just a terse if ... else.
Nov 14 '05 #2
"Henning" <co***********@coldmail.com> wrote in message
news:40***********************@news3.bahnhof.se...
I'm translating a prog written in IAR C for AVR.
But my knowledge in C is too low to understand this.

//================================================== ======================== ===
// Sets or clears the bit 'index' in the bit table 'table', regarding the
value
// of 'value'.

void putBit(unsigned char *table, unsigned char index, unsigned char
value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?


This:
expr1 = expr2 ? expr3 : expr4;
Is equivalent to this:
if (expr2) expr1 = expr3; else expr1 = expr4;

Personally, I would have written the above as (IMO more idiomatic):
if (value) *p |= m; else *p &= ~m;

Alex
Nov 14 '05 #3

"Malcolm" <ma*****@55bank.freeserve.co.uk> skrev i meddelandet
news:cd*********@news8.svr.pol.co.uk...

"Henning" <co***********@coldmail.com> wrote

void putBit(unsigned char *table, unsigned char index, unsigned char

value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?

*p - take the memory location pointed to by p
= - write to it
(value) ? x : y - if value is non-zero write x, else write y
m | *p - logical OR of m and *p
~m & *p - logical AND of complement of m and *p

the ? : syntax is just a terse if ... else.

Thanx a lot! might struggle into more q's later.
/Henning
Nov 14 '05 #4
"Henning" <co***********@coldmail.com> wrote in message news:<40***********************@news3.bahnhof.se>. ..
<snip>
But my knowledge in C is too low to understand this.
<snip>
What does *p = (value) ? ...... do?


Let's see... You obviously don't know C, and the people here aren't
of the disposition to teach it to you.

So get yourself a book on C (see FAQ question 18.10), and read it.
"Build a man a fire, and he's warm for a day. Set a man on fire, and
he's warm for the rest of his life." - StarChaser, Sep 04 2000
Mark F. Haigh
Nov 14 '05 #5
Mark F. Haigh wrote:
"Henning" <co***********@coldmail.com> wrote in message news:<40***********************@news3.bahnhof.se>. ..
<snip>
But my knowledge in C is too low to understand this.
<snip>
What does *p = (value) ? ...... do?


Let's see... You obviously don't know C, and the people here aren't
of the disposition to teach it to you.


A little abrupt. People here are very willing to help a C novice
advance his or her understanding -- given evidence that the novice
has put in a modicum of effort. I'm not sure the condition is
satisfied in this case, but:

*p means (roughly) "the content of p", where p is implicitly a
pointer to some sort of object. So, for example, if p is a
pointer-to-int, then

*p = 3;

says "store the integer value 3 at the memory address indicated by
the pointer variable p."
So get yourself a book on C (see FAQ question 18.10), and read it.


Good advice.

Allin Cottrell.
Nov 14 '05 #6
Allin Cottrell <co******@wfu.edu> writes:
[...]
*p means (roughly) "the content of p", where p is implicitly a
pointer to some sort of object.


I think "the content of p" is a poor choice of words. Normally that
would refer to the value of the variable p, which is a pointer value.

*p refers to the object to which p points.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

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

Similar topics

7
by: Bengt Richter | last post by:
Just thought None as the first argument would be both handy and mnemonic, signifying no translation, but allowing easy expression of deleting characters, e.g., s = s.translate(None,...
1
by: shank | last post by:
I'm sure this is a stretch, but is there some kind of component that I could install to translate from English to Spanish on the fly? I have a lot of equipment features and specifications that I...
4
by: Gadrin77 | last post by:
I have data that looks like <Root> <Main Value="Line1|Line2.|Line3|Line4.|Line5"/> </Root> I'm using Translate(@Value, "|.", ",")
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
1
by: peterbe | last post by:
This has always worked fine for me. Peter fine Now if I do it with a unicode string: Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/string.py", line...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
1
by: =?Utf-8?B?R2F1cmF2?= | last post by:
Hi, I am using the Translate() function in one of the .XSLT file to remove the spaces, like this: <xsl:for-each select=".//Illustration"> <xsl:value-of select="translate(./@illusName, ' ',...
3
by: Kenneth McDonald | last post by:
I have the need to occasionally translate a single word programatically. Would anyone have a Python script that would let me do this using Google (or another) translation service? Thanks, Ken
4
by: kovariadam | last post by:
Hi, Does anybody know why i get this error: SQL0176N The second, third or fourth argument of the TRANSLATE scalar function is incorrect. SQLSTATE=42815 with this query: SELECT...
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...
0
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,...

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.