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

simplification of function

GTi
Can this be done different in C#

private ushort GetRQU(ushort regpos)
{
ushort pos;
ushort tmp;
ushort value = 0;
string inBuff = "AA"; // EXAMPLE ONLY

tmp = (ushort)inBuff[0];
if(( tmp & 1 ) != 0) { value = (ushort)( value + 32768 ); }
if(( tmp & 2 ) != 0) { value = (ushort)( value + 16384 ); }
if(( tmp & 4 ) != 0) { value = (ushort)( value + 8192 ); }
if(( tmp & 8 ) != 0) { value = (ushort)( value + 4096 ); }
if(( tmp & 16 ) != 0) { value = (ushort)( value + 2048 ); }
if(( tmp & 32 ) != 0) { value = (ushort)( value + 1024 ); }
if(( tmp & 64 ) != 0) { value = (ushort)( value + 512 ); }
if(( tmp & 128 ) != 0) { value = (ushort)( value + 256 ); }
tmp = inBuff[1];
if(( tmp & 1 ) != 0) { value = (ushort)(value + 128); }
if(( tmp & 2 ) != 0) { value = (ushort)(value + 64); }
if(( tmp & 4 ) != 0) { value = (ushort)(value + 32); }
if(( tmp & 8 ) != 0) { value = (ushort)(value + 16); }
if(( tmp & 16 ) != 0) { value = (ushort)(value + 8); }
if(( tmp & 32 ) != 0) { value = (ushort)(value + 4); }
if(( tmp & 64 ) != 0) { value = (ushort)(value + 2); }
if(( tmp & 128 ) != 0) { value = (ushort)( value + 1 ); }
return ( value );
}
And a function that do it the other way back:
(this is a C function)

BYTE ComliSetReg(short a, unsigned short b )
{
BYTE ret;

ret=0;
if(a==0) /* LOW */
{
if(b & 1) ret = ret + 128;
if(b & 2) ret = ret + 64;
if(b & 4) ret = ret + 32;
if(b & 8) ret = ret + 16;
if(b & 16) ret = ret + 8;
if(b & 32) ret = ret + 4;
if(b & 64) ret = ret + 2;
if(b & 128) ret = ret + 1;
}
else /* HIGH */
{
if(b & 256) ret = ret + 128;
if(b & 512) ret = ret + 64;
if(b & 1024) ret = ret + 32;
if(b & 2048) ret = ret + 16;
if(b & 4096) ret = ret + 8;
if(b & 8192) ret = ret + 4;
if(b & 16384) ret = ret + 2;
if(b & 32768) ret = ret + 1;
}

return(ret);
}

Aug 31 '06 #1
2 1171
What is it meant to do? regpos and pos aren't used in GetRQU, but otherwise
(and I may be reading it wrong here) it /looks/ like it simply reverses the
bits (left-to-right) - in which case surely the "undo" is to run it again?

Anyway, the answer is probably to use bit operators in a loop - i.e.
something like (in pseudo#)

uint currentBit = 32768;
for(each byte) {
for(0 to 7) {
if((tmp & 1) == 1) value |= currentBit; // add to mask
tmp >>= 1; // test next bit
add >>= 1; // halve currentBit
}
}

Marc
Aug 31 '06 #2
Hi,

You can use a loop, apparentely you go checking one bit at a time and
depending of its status you add certain value

you should use int instead of ushort for value and doing just a conversion
at the end.

also the C function should work with only minimal changes.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"GTi" <tu****@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Can this be done different in C#

private ushort GetRQU(ushort regpos)
{
ushort pos;
ushort tmp;
ushort value = 0;
string inBuff = "AA"; // EXAMPLE ONLY

tmp = (ushort)inBuff[0];
if(( tmp & 1 ) != 0) { value = (ushort)( value + 32768 ); }
if(( tmp & 2 ) != 0) { value = (ushort)( value + 16384 ); }
if(( tmp & 4 ) != 0) { value = (ushort)( value + 8192 ); }
if(( tmp & 8 ) != 0) { value = (ushort)( value + 4096 ); }
if(( tmp & 16 ) != 0) { value = (ushort)( value + 2048 ); }
if(( tmp & 32 ) != 0) { value = (ushort)( value + 1024 ); }
if(( tmp & 64 ) != 0) { value = (ushort)( value + 512 ); }
if(( tmp & 128 ) != 0) { value = (ushort)( value + 256 ); }
tmp = inBuff[1];
if(( tmp & 1 ) != 0) { value = (ushort)(value + 128); }
if(( tmp & 2 ) != 0) { value = (ushort)(value + 64); }
if(( tmp & 4 ) != 0) { value = (ushort)(value + 32); }
if(( tmp & 8 ) != 0) { value = (ushort)(value + 16); }
if(( tmp & 16 ) != 0) { value = (ushort)(value + 8); }
if(( tmp & 32 ) != 0) { value = (ushort)(value + 4); }
if(( tmp & 64 ) != 0) { value = (ushort)(value + 2); }
if(( tmp & 128 ) != 0) { value = (ushort)( value + 1 ); }
return ( value );
}
And a function that do it the other way back:
(this is a C function)

BYTE ComliSetReg(short a, unsigned short b )
{
BYTE ret;

ret=0;
if(a==0) /* LOW */
{
if(b & 1) ret = ret + 128;
if(b & 2) ret = ret + 64;
if(b & 4) ret = ret + 32;
if(b & 8) ret = ret + 16;
if(b & 16) ret = ret + 8;
if(b & 32) ret = ret + 4;
if(b & 64) ret = ret + 2;
if(b & 128) ret = ret + 1;
}
else /* HIGH */
{
if(b & 256) ret = ret + 128;
if(b & 512) ret = ret + 64;
if(b & 1024) ret = ret + 32;
if(b & 2048) ret = ret + 16;
if(b & 4096) ret = ret + 8;
if(b & 8192) ret = ret + 4;
if(b & 16384) ret = ret + 2;
if(b & 32768) ret = ret + 1;
}

return(ret);
}

Aug 31 '06 #3

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
5
by: John | last post by:
How could I simplify the code to get libs out of LDFLAGS or vice versa automatically in the following python/scons code? if sys.platform == 'linux': env.Append (CPPFLAGS = '-D__LINUX')...
3
by: bearophileHUGS | last post by:
The current version of ShedSkin (http://shedskin.sourceforge.net/ experimental Python to C++ compiler) is rather alpha, and the development work is focused on debugging and implementing some more...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
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
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
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
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...
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.