Binary to Decimal Using Chash

Samual Sam
Updated on 19-Jun-2020 08:18:40

313 Views

To convert binary to decimal, here I have used a while loop and found the remainder of the Binary number, which is the input. After that, the remainder is multiplied by the base value and added.This is what I did to get the decimal value −while (val > 0) {    remainder = val % 10;    myDecimal = myDecimal + remainder* baseVal;    val = val / 10;    baseVal = baseVal * 2; }ExampleLet us see the complete code to convert binary to decimal in C# −Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class ... Read More

Bitwise Right Shift Operators in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:18:07

794 Views

Bitwise operator works on bits and performs bit by bit operation. In Bitwise right shift operator the value of the left operand is moved right by the number of bits specified by the right operand.In the below code, we have the value −60 i.e. 0011 1100On the right shift %minus;c = a >> 2;It converts into 15 after right shift twice −15 i.e. 0000 1111ExampleYou can try to run the following code to implement Bitwise right shift operator in C# −using System; using System.Collections.Generic; using System.Text; namespace Demo {    class toBinary {       static void Main(string[] args) ... Read More

Check if Binary Representation is Palindrome in C#

Samual Sam
Updated on 19-Jun-2020 08:15:05

353 Views

To check for palindrome, let us say our number is 5, whose binary is −101The palindrome of 101 is 101 and to check you need to reverse the bits using the following function. Here, bitwise left and bitwise right shift operators are used −public static long funcReverse(long num) {    long myRev = 0;    while (num > 0) {       myRev = 1;    }    return myRev; }Then the actual representation will be compared be the reverse one by returning and getting the value from the funcReverse() function −public static bool checkPalindrome(long num) {    long ... Read More

Assertions in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:14:00

2K+ Views

Assert statements are an effective way to catch program logic errors at runtime. It has two arguments −A boolean expression for a true condition, andWhat to display in case of false.Assertions are useful in large and complex programs to quickly flush out errors that generally arise when the code is modified. Avoid using any function call inside the assert method.You need to be sure that whatever code you add inside an Assert should not change the output if it is removed. This is when you implement Debug. Assert in your program.To implement it, you can use a temporary variable −int ... Read More

Are Arrays Zero-Indexed in C#

Samual Sam
Updated on 19-Jun-2020 08:13:38

1K+ Views

Yes, arrays zero indexed in C#. Let us see how −If the array is empty, it has zero elements and has length 0.If the array has one element in 0 indexes, then it has length 1.If the array has two elements in 0 and 1 indexes, then it has length 2.If the array has three elements in 0, 1 and 2 indexes, then it has length 3.The following states that an array in C# begins with index 0 −/* begin from index 0 */ for ( i = 0; i < 5; i++ ) {    n[ i ] = ... Read More

Accessing Attributes and Methods in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:12:55

429 Views

An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. To set an attribute −[attribute(positional_parameters, name_parameter = value, ...)] ElementHere, the name of the attribute and values come inside [ ] positional parameters allow you to specify information.ExampleThe following is an example to access attribute and methods in C# −Live Demo#define DEBUG using System; using System.Diagnostics; public class Demo {    [Conditional("DEBUG")]    public static void Message(string str) {       Console.WriteLine(str);    } } class Test {    static ... Read More

Abstract Keyword in C#

Samual Sam
Updated on 19-Jun-2020 08:12:01

694 Views

The abstract keyword in C# is used for abstract classes. An abstract class in C# includes abstract and nonabstract methods. You cannot instantiate an abstract class.Example of an abstract class Vehicle and abstract method display() −public abstract class Vehicle {    public abstract void display(); }The abstract class has derived classes: Bus, Car, and Motorcycle. The following is an implementation of the Car derived class −public class Car : Vehicle {    public override void display() {       Console.WriteLine("Car");    } }ExampleThe following is an example of abstract classes in C# −Live Demousing System; public abstract class Vehicle ... Read More

Addition and Concatenation in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:11:15

343 Views

To add and concatenate strings in C#, use the string. Concat method. The plus operator can also be used for the same purpose of concatenation.Plus Operatorstring str2 = "Hanks" + str1;ExampleLet us see an example of + operator to concatenate strings −Live Demousing System; class Program {    static void Main() {       string str1 = "Tom";       // concatenation       string str2 = "Hanks" + str1;       Console.WriteLine(str2);    } }OutputHanksTomString.concatstring str2 = string.Concat("Hanks", str1);ExampleLet us see an example of string.concat to concatenate strings in C# −Live Demousing ... Read More

Abstract vs Sealed Classes vs Class Members in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:09:12

1K+ Views

The abstract class includes abstract and non-abstract methods. You cannot instantiate an abstract class.The sealed class prevents inheritance and you cannot use it as a base class.Abstract ClassesTo declare an abstract class, you need to place the keyword abstract before the class definition. An example of class members in an abstract class can be the following that defines an abstract method −public abstract class Vehicle {    public abstract void display(); }The abstract method definition is followed by a semi-colon since it has no implementation.Sealed ClassesTo declare a sealed class, you need to place the keyword sealed before the class definition. ... Read More

Three Different Ways to Calculate Factorial in C#

Samual Sam
Updated on 19-Jun-2020 08:08:47

777 Views

To calculate a factorial in C#, you can use any of the following three ways −Calculate factorial with for loopExampleLive Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace factorial {    class Test {       static void Main(string[] args) {          int i, res;          int value = 5;          res = value;          for (i = value - 1; i >= 1; i--) {             res = res * i;          }       ... Read More

Advertisements