473,394 Members | 1,737 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,394 software developers and data experts.

Extract unsigned values from certain bits in C# ushort

Hi all.

I need a good way of extracting values from certain bits inside a unsigned short:


The definition of the 4 alarms inside the ushort:

Name......Range.......Bits
Alarm4....0-15....13/16-16/16
Alarm3....0-15....9/16-12/16
Alarm2....0-15....5/16-8/16
Alarm1....0-15....1/16-4/16


The value of the ushort: 4383 or (1000100011111 in binary)


So what i want to achieve is:
1001100110011001
Alarm1.....Alarm2.....Alarm3......Alarm4
1001.......1001.......1001........1001

Gets translated into:
Alarm1....Alarm2....Alarm3....Alarm4
9............9............9............9


Using the pseudo code:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  getValueFunc(ushort bits, int offset);
  3.  
  4.  ushort u = 4383;
  5.  UInt16 Alarm1Value = getValueFunc(u, 1);
  6.  UInt16 Alarm2Value = getValueFunc(u, 5);
  7.  UInt16 Alarm3Value = getValueFunc(u, 9);
  8.  UInt16 Alarm4Value = getValueFunc(u, 13);
  9.  
  10.  
  11.  

Thank you very much in advance for your time.

Regards,
Johan
Nov 18 '10 #1

✓ answered by Christian Binder

Taking the second four bits:
1001100110011001 if you & (and) it with
0000000011110000 you'll get
0000000010010000 when you shift this thing by four places,
0000000000001001 you'll get what you want

this would look like
Expand|Select|Wrap|Line Numbers
  1. ushort getValueFunc(ushort bits, int offset) {
  2.   //assume offset starts by zero (right four bytes)
  3.   //bits:   1001 1001 1001 1001
  4.   //offset:  3    2    1    0
  5.   //result:  9    9    9    9
  6.  
  7.   ushort and_value = 15; // 1111
  8.   and_value <<= offset * 4;
  9.     //move it offset*4 binary places to the left
  10.     //offset  and_value
  11.         0                   1111
  12.         1               11110000
  13.         2           111100000000
  14.         3       1111000000000000
  15.  
  16.  
  17.   bits &= and_value; //and-ing
  18.  
  19.   bits >>= offset * 4:
  20.   //move it back offset*4 binary places to the right
  21.  
  22.   return bits;
  23. }
  24.  
I hope this works and helps :-)

3 3868
Christian Binder
218 Expert 100+
Taking the second four bits:
1001100110011001 if you & (and) it with
0000000011110000 you'll get
0000000010010000 when you shift this thing by four places,
0000000000001001 you'll get what you want

this would look like
Expand|Select|Wrap|Line Numbers
  1. ushort getValueFunc(ushort bits, int offset) {
  2.   //assume offset starts by zero (right four bytes)
  3.   //bits:   1001 1001 1001 1001
  4.   //offset:  3    2    1    0
  5.   //result:  9    9    9    9
  6.  
  7.   ushort and_value = 15; // 1111
  8.   and_value <<= offset * 4;
  9.     //move it offset*4 binary places to the left
  10.     //offset  and_value
  11.         0                   1111
  12.         1               11110000
  13.         2           111100000000
  14.         3       1111000000000000
  15.  
  16.  
  17.   bits &= and_value; //and-ing
  18.  
  19.   bits >>= offset * 4:
  20.   //move it back offset*4 binary places to the right
  21.  
  22.   return bits;
  23. }
  24.  
I hope this works and helps :-)
Nov 18 '10 #2
Just tested it, works 100%.

Thank you very much for the solution and explaination, now it makes sense for people like me with little binary experience :)
Nov 18 '10 #3
Christian Binder
218 Expert 100+
Fine :-) you're welcome
Nov 18 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
3
by: BigMan | last post by:
Let's consider the following piece of code: unsigned int ui; std::istringstream StrStream( "-1" ); StrStream >> ui; How do I catch the case when StrStream contains negative numbers?
6
by: Christopher Benson-Manica | last post by:
int main() { bool foo=false; for( unsigned int bar=0; bar < (foo?1:2); bar++ ) { bar=42; } return 0; } A certain C++ compiler I am using claims that there exists in the
9
by: Thiru .Net | last post by:
how to assign unsigned values in vb.net i have a statement like this in c# public const uint FEEDER = 0x00000001; when i tried to convert above into vb.net i said Public Const FEEDER As...
1
by: roberto321 | last post by:
Hi Guys, I was wondering if someone could help me out with the following requirements <mydocument> <div id="other"> <a href="linkother">linkother</a> </div> <div id="hello">
2
by: Pete | last post by:
Hi, First, thanks for any time you spend helping me, I'm at a loss. I'm not bit-savvy, so I apologize if this is extremely simple, or I am going about this the wrong way. I am trying to take a...
13
by: Martin Wells | last post by:
I commonly use the likes of -1 for max unsigned values, or something like -7 to get 6 away from the max value; sort of like: unsigned x = ... if (-7 == x) puts("x is 6 away from its maximum");...
2
by: dh87lfc | last post by:
Hi, I am currently working on a task which requires me to extract the top 10 maximum values from a column in a text file and print them to a new file. I am just wondering what would be the best way...
4
by: rkmr.em | last post by:
hi i want to send unsigned 32 bit integer to socket, and looking for something equivalent to this method... http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt() ...
5
by: sathumenon | last post by:
Hi Am a new bie - and in need of some helo Please find the attachment here. I would like to know how I can extract the values from this and list properly? What I need is to list the Hotel...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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...

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.