Categories

logical operators in C sharp

Logical

The following logical operators operate on boolean or integral operands, as noted.

Sample usage Read Type Explanation
a&b a bitwise and b binary & evaluates both of its operands and returns the logical conjunction (“AND”) of their results. If the operands are integral, the logical conjunction is performed bitwise.
a&&b and b binary && operates on boolean operands only. It evaluates its first operand. If the result is false, it returns false. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical conjunction performed by the & operator. This is an example of Short Circuit Evaluation.
a | b a bitwise or b binary | evaluates both of its operands and returns the logical disjunction (“OR”) of their results. If the operands are integral, the logical disjunction is performed bitwise.
a || b or b binary || operates on boolean operands only. It evaluates the first operand. If the result is true, it returns true. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical disjunction performed by the | operator. This is an example of Short Circuit Evaluation.
a ^ b x-orb binary ^ returns the exclusive or (“XOR”) of their results. If the operands are integral, the exclusive or is performed bitwise.
!a not a unary ! operates on a boolean operand only. It evaluates its operand and returns the negation (“NOT”) of the result. That is, it returns true if a evaluates to false and it returns false if a evaluates totrue.
~a bitwise not a unary ~ operates on integral operands only. It evaluates its operand and returns the bitwise negation of the result. That is, ~a returns a value where each bit is the negation of the corresponding bit in the result of evaluating a.

arithmetic operators in C sharp

Arithmetic

The following arithmetic operators operate on numeric operands (arguments a and b in the “sample usage” below).

Sample usage Read Type Explanation
a + b plus b binary + returns the sum of its arguments.
a - b minus b binary - returns the difference between its arguments.
a*b times b binary * returns the multiplicative product of its arguments.
a/b divided by b binary / returns the quotient of its arguments. If both of its operators are integers, it obtains that quotient using integer division (i.e. it drops any resulting remainder).
a%b mod b binary % operates only on integer arguments. It returns the remainder of integer division of those arguments. (See modular arithmetic.)
a++ plus plus or Postincrement a unary ++ operates only on arguments that have an l-value. When placed after its argument, it increments that argument by 1 and returns the value of that argument before it was incremented.
++a plus plus a or Preincrement a unary ++ operates only on arguments that have an l-value. When placed before its argument, it increments that argument by 1 and returns the resulting value.
a– minus minus orPostdecrement a unary – operates only on arguments that have an l-value. When placed after its argument, it decrements that argument by 1 and returns the value of that argument before it was decremented.
–a minus minus a orPredecrement a unary – operates only on arguments that have an l-value. When placed before its argument, it decrements that argument by 1 and returns the resulting value.

C compilers

Free version available
Ch_interpreter (http://www.softintegration.com) – The software works in Windows, Linux, Mac OS X, Freebsd, Solaris, AIX and HP-UX. The Ch Standard Edition is free for noncommercial use.

lcc-win32 (http://www.cs.virginia.edu/~lcc-win32) – Software copyrighted by Jacob Navia. It is free for non-commercial use. Windows (98/ME/XP/2000/NT).

GNU Compiler Collection (http://gcc.gnu.org) – GNU Compiler Collection. GNU General Public License / GNU Lesser General Public License.

MinGW (http://www.mingw.org/) provides GCC for Windows

Open Watcom (http://www.openwatcom.org) Open Source development community to maintain and enhance the Watcom C/C++ and Fortran cross compilers and tools.

Tiny C Compiler (http://bellard.org/tcc/)

Portable C Compiler (http://pcc.ludd.ltu.se) – Portable C Compiler. BSD Style License(s).

Small Device C Compiler (http://sdcc.sourceforge.net/) target platforms: Intel 8051-compatibles; Freescale (Motorola) HC08; Microchip PIC16 and PIC18. FpgaC. Target platform: FPGA hardware via XNF or VHDL files.

Interactive C (http://www.botball.org/educational-resources/ic.php). target platform: Handy Board (Freescale 68HC11); Lego RCX
C compilers for many digital signal processors (DSPs), many of them free, are listed in the comp.dsp

Commercial
Intel C Compiler (http://software.intel.com/en-us/intel-compilers) – Windows, Linux, Mac, QNX, and embedded C/C++ compilers. Optimized for Intel 32-bit and 64-bit CPUs.

Microsoft Visual C++ (http://msdn.microsoft.com/visualc) – Free (partially limited) version available (Express edition)

Impulse C (http://www.impulsec.com/)- Target platform: FPGA hardware via Hardware Description Language (HDL) files.

Creating a windows service with C sharp

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

public class WinService1 : System.ServiceProcess.ServiceBase {
private EventLog eventLog;

public WinService1() {
this.ServiceName = “WinService1″;

string source = “Main”;
eventLog = new EventLog();
eventLog.Source = source;
}
static void Main() {
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args) {
eventLog.WriteEntry(“starting up!”);
}
protected override void OnStop() {
eventLog.WriteEntry(“shutting down!”);
}
}

Get OS Version from kernel32.dll with C sharp

using System;
using System.Runtime.InteropServices;

public class Starter {
public static void Main() {
API.OSVERSIONINFO info = new API.OSVERSIONINFO();
info.dwOSVersionInfoSize = Marshal.SizeOf(info);
bool resp = API.GetVersionEx(ref info);
if (resp == false) {
Console.WriteLine(“GetVersion failed”);
}
Console.WriteLine(“{0}.{1}.{2}”,
info.dwMajorVersion,
info.dwMinorVersion,
info.dwBuildNumber);
}
}

public class API {

[DllImport("kernel32.dll")]
public static extern
bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo);

[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO {
public System.Int32 dwOSVersionInfoSize;
public System.Int32 dwMajorVersion;
public System.Int32 dwMinorVersion;
public System.Int32 dwBuildNumber;
public System.Int32 dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public String szCSDVersion;

}

}

Ping example in C sharp

using System;
using System.Net.NetworkInformation;

class MainClass {
public static void Main(string[] args) {
using (Ping ping = new Ping()) {
Console.WriteLine(“Pinging:”);

foreach (string comp in args) {
try {
Console.Write(” {0}…”, comp);
PingReply reply = ping.Send(comp, 100);
if (reply.Status == IPStatus.Success) {
Console.WriteLine(“Success – IP Address:{0}”, reply.Address, reply.RoundtripTime);
} else {
Console.WriteLine(reply.Status);
}
} catch (Exception ex) {
Console.WriteLine(“Error ({0})”,
ex.InnerException.Message);
}
}
}
}
}