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.
Photo

binary number representation in c#


  • Please log in to reply
5 replies to this topic

#1 mastronic

mastronic

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrench - France (Lille)

Posted 01 June 2012 - 10:21 AM

Hello, how do you represent a binary number in c#?? I know for a hex number is = 0xffffffff but what about binary? Regards. Thierry

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 01 June 2012 - 11:10 AM

C# does not support binary literals. There are a few tricks that can be used, but the question is why would you need that? The conversion between hex and binary is trivial; besides it is considered a good practice to use named values (constants, enums etc.) instead of hardcoded 'magic numbers'.

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 01 June 2012 - 11:51 AM

CW2 is right.
Languages such the C# provide a good level of abstraction, so that you should not rely on how the number is internally represented.
Thinking a number as binary is just a shortcut, but none can guarantee anything about the relation.
A certain MF implementation could store a number as hex digits, or even using a double-precision. In such a case, the binary representation is not related to an integer number.

A similar wrong assumption has been made for the construction of an IPAddress instance.
http://msdn.microsof...y/hh434586.aspx
The components' value can be given either in the byte-array form (correct), and as Int64 (wrong).

For a tiny environment as the MF is, of course that assumption might be accepted (upon risk). On a PC I'd be much more drastic, by alerting/blocking any potential erratic assumption.
Some example:
- integer cast from/to an Enum;
- any bitwise operator (&, <<, >>, ^, etc)
- any unsigned type (byte should be used only for data interop)

Cheers
Biggest fault of Netduino? It runs by electricity.

#4 mastronic

mastronic

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationFrench - France (Lille)

Posted 02 June 2012 - 08:08 AM

Thank you.
Binary notation, is sometimes handy while writing a code to access a register where each bit to a property.


A simply solution:

        const byte b00000000 = 0x00;
        const byte b00000001 = 0x01;
        const byte b00000010 = 0x02;
        const byte b00000011 = 0x03;
        const byte b00000100 = 0x04;
....
Attachments file for all value;


Use For exemple to circuit test (use class b)..
            i2c_gyro.Write((byte) register.CTRL_REG1, b.b00001111);                    // Turn on XYZ axes
            //  Bit 7, 6:  Output Data Rate Selection      [100Hz, 200Hz, 400Hz, 800Hz]                                                                                       
            //  Bit 5, 4:  Bandwidht selection      [12.5 To 110  fonction ODR selection]                            
            //  Bit 3:  Power Down if 0
            //  Bit 2:  Z Axis Enable
            //  Bit 1:  Y Axis Enable
            //  Bit 0:  X Axis Enable

it' équivalent to:
i2c_gyro.Write((byte) register.CTRL_REG1, (byte) 0x0F);                    // Turn on XYZ axes


Thierry

Attached Files



#5 vroby67

vroby67

    New Member

  • Members
  • Pip
  • 1 posts
  • LocationTrofarello (TO) italia - 44.979502,7.759105

Posted 29 June 2012 - 08:48 PM

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


Roberto Visca, sc
REM snc

#6 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 30 June 2012 - 05:45 PM

As other have already said i always use a const or readonly private variables for both expected results and for bit masks. I generally find that simple Bitwise operators are easier to use, as the overhead of a class to handle it usually isnt worth it IMHO on the netduino (or any other platform with tight memory restrictions) Nak.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.