Bitwise operations in C

Bitwise operators in C are & AND Operator | OR Operator ~ NOT Operator ^ XOR Operator The code below demonstrate common bitwise operations in c # include<stdio.h> int main(){ int a, b, option, res; printf("Enter a and b\n"); scanf("%d %d", &a, &b); do{ printf("MENU\n 1.AND\n 2.OR\n 3.NOT \n 4.XOR\n 0.Exit\n"); printf("Enter choice: "); scanf("%d", &option); switch(option){ case 1: res = a & b; printf("%d AND %d = %d\n", a, b, res); break; case 2: res = a | b; printf("%d OR %d = %d\n", a, b, res); break; case 3: res = ~a; printf("NOT of %d = %d\n", a, res); res = ~b; printf("NOT of %d = %d\n", b, res); break; case 4: res = a ^ b; printf("%d XOR %d = %d\n", a, b, res); break; default: break; } printf("\n"); }while(option); return 0; } Output: ...

January 1, 2015 · 1 min · Zeeshan Khan

Swap Two Numbers in C

Code below swaps two numbers without using a third variable # include<stdio.h> int swap(int* a, int *b){ // swapping values using match trick *a = *a + *b; *b = *a - *b; *a = *a - *b; return 0; } int main(){ int a, b; printf("Enter two numbers"); scanf("%d %d", &a, &b); printf("a=%d, b=%d", a, b); swap(&a, &b); printf("a=%d, b=%d", a, b); return 0; } Output: Enter two numbers 10 34 before swapping a=10, b=34 After swapping a=34, b=10

December 30, 2014 · 1 min · Zeeshan Khan

Add Two Numbers in C

To add two numeric values in C #include<stdio.h> int main(){ float a, b; printf("Enter two numbers\n"); scanf("%f %f",&a,&b); printf("sum of %f and %f is %f",a,b,a+b); return 0; } Output: Enter two numbers 12 34 sum of 12.000000 and 34.000000 is 46.000000

December 28, 2014 · 1 min · Zeeshan Khan

Add Two Integers in Java

Following program will add two Integer numbers for you. // file Addition.java import java.io.*; class Addition{ public static void main(String args[]) throws IOException{ int a,b; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter first integer"); a=Integer.parseInt(br.readLine()); System.out.println("Enter second integer"); b=Integer.parseInt(br.readLine()); System.out.println("Sum of two integers is: "+(a+b)); } } To run the above code navigate to the source code folder from cmd(Windows) or terminal(Linux) then type the following code to compile javac <filename>.java after sucessfully compiling, type java Addition to start the programe. ...

December 28, 2014 · 1 min · Zeeshan Khan