Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

vroby67's Content

There have been 1 items by vroby67 (Search limited from 29-June 23)


By content type

See this member's

Sort by                Order  

#31345 binary number representation in c#

Posted by vroby67 on 29 June 2012 - 08:48 PM in Visual Studio

Using single bit is a job of mine. We develop application in test benches applications, so usually need to switch on, off and test a bit in a register. I developed an object called bitInt. It make possible access a specific bit in the following form:
  public BitInt[] We = new BitInt[4];
  public BitInt[] Wu = new BitInt[4];
...
  We[0].Value = ....; //read hw input register

  Wu[0].Set(7);
  Wu[0].Rst(8);
  Wu[0].Ld(9, (We[0].Get(0) | Wu[0].Get(9)) & !We[0].Get(2));

  writeHwRegister(Wu[0].Value);


In this example, I defined two arrays of four elements, the first one for reading input registers and the other to write the output registers.
The Set(int n) method, set the n(th) bit to true.
The Rst(int n) method, set the n(th) bit to false.
the Ld(int n, bool val) set the n(th) bit to the boolean state you pass, in this sample a start stop control.

A good way to improve flexibility and code interpretation, consists of using enum for identifying signals (bit) inside the registers


public enum we0names
{
  powerOn
  ,alarm
  ,ready
  ,startBtn
  ,stopBtn
  ,nc5
  ,forwardSwitch
  ,nc7
  ,reverseSwitch
}


...then replacing the numbers in the method call with enum reference

  public BitInt[] We = new BitInt[4];
  public BitInt[] Wu = new BitInt[4];
...
  We[0].Value = ....; //read hw input register

  Wu[0].Set(7);
  Wu[0].Rst(8);
  Wu[0].Ld(9, (We[0].Get((int)we0names.power) | Wu[0].Get(9)) & !We[0].Get((int)we0names.ready));

  writeHwRegister(Wu[0].Value);


If you are interested in the source, let me know.

Bye

Attached Files





home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.