Hello,
how do you represent a binary number in c#??
I know for a hex number is = 0xffffffff but what about binary?
Regards.
Thierry
binary number representation in c#
Started by mastronic, Jun 01 2012 10:21 AM
5 replies to this topic
#1
Posted 01 June 2012 - 10:21 AM
#2
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'.
- mastronic likes this
#3
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
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
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:
Use For exemple to circuit test (use class b)..
it' équivalent to:
Thierry
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
- st3mcg likes this
#5
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:
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
...then replacing the numbers in the method call with enum reference
If you are interested in the source, let me know.
Bye
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
REM snc
#6
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