banner



Which Of The Following Commands ​creates An Alternate Data Stream?

Bitwise Operators in C/C++

In C, the following 6 operators are bitwise operators (work at bit-level)

Bitwise Operators in C/C++

  1. The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
  2. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The issue of OR is i if any of the two $.25 is i.
  3. The ^ (bitwise XOR) in C or C++ takes 2 numbers as operands and does XOR on equally of 2 numbers. The event of XOR is 1 if the two $.25 are dissimilar.
  4. The << (left shift) in C or C++ takes two numbers, left shifts the $.25 of the first operand, the second operand decides the number of places to shift.
  5. The >> (right shift) in C or C++ takes two numbers, correct shifts the $.25 of the beginning operand, the 2nd operand decides the number of places to shift.
  6. The ~ (bitwise Not) in C or C++ takes one number and inverts all bits of information technology.

Example:

C++

#include <iostream>

using namespace std;

int main() {

int a = 5, b = ix;

cout<< "a = " << a << "," << " b = " << b <<endl;

cout << "a & b = " << (a & b) << endl;

cout << "a | b = " << (a | b) << endl;

cout << "a ^ b = " << (a ^ b) << endl;

cout << "~a = " << (~a) << endl;

cout<< "b << i" << " = " << (b << 1) <<endl;

cout<< "b >> 1 " << "= " << (b >> i )<<endl;

render 0;

}

C

#include <stdio.h>

int main()

{

unsigned char a = v, b = ix;

printf ( "a = %d, b = %d\n" , a, b);

printf ( "a&b = %d\n" , a & b);

printf ( "a|b = %d\north" , a | b);

printf ( "a^b = %d\north" , a ^ b);

printf ( "~a = %d\n" , a = ~a);

printf ( "b<<1 = %d\n" , b << 1);

printf ( "b>>1 = %d\northward" , b >> 1);

return 0;

}

Output

a = 5, b = 9 a & b = one a | b = thirteen a ^ b = 12 ~a = -vi b << one = 18 b >> 1 = 4

Interesting facts nearly bitwise operators

  1. The left shift and right shift operators should non exist used for negative numbers. If the 2d operand(which decides the number of shifts) is a negative number, information technology results in undefined behaviour in C. For example results of both 1 <<- i and 1 >> -1 is undefined. As well, if the number is shifted more than than the size of the integer, the behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. Another affair is, NO shift operation is performed if additive-expression(operand that decides no of shifts) is 0. See this for more details.
    Note: In C++, this beliefs is well-defined.
  2. The bitwise XOR operator is the most useful operator from a technical interview perspective. Information technology is used in many problems. A simple example could be "Given a fix of numbers where all elements occur an even number of times except one number, observe the odd occurring number" This problem can be efficiently solved by just doing XOR to all numbers.

C++

#include <iostream>

using namespace std;

int findOdd( int arr[], int north)

{

int res = 0, i;

for (i = 0; i < n; i++)

res ^= arr[i];

return res;

}

int main( void )

{

int arr[] = { 12, 12, 14, ninety, fourteen, 14, fourteen };

int n = sizeof (arr) / sizeof (arr[0]);

cout << "The odd occurring chemical element is  " << findOdd(arr, northward);

return 0;

}

C

#include <stdio.h>

int findOdd( int arr[], int n)

{

int res = 0, i;

for (i = 0; i < north; i++)

res ^= arr[i];

return res;

}

int primary( void )

{

int arr[] = { 12, 12, 14, 90, 14, 14, 14 };

int n = sizeof (arr) / sizeof (arr[0]);

printf ( "The odd occurring chemical element is %d " ,

findOdd(arr, due north));

return 0;

}

Output

The odd occurring element is  90

The following are many other interesting bug using XOR operator.

  1. Discover the Missing Number
  2. swap two numbers without using a temporary variable
  3. A Memory Efficient Doubly Linked List
  4. Notice the two non-repeating elements.
  5. Find the ii numbers with odd occurences in an unsorted-array.
  6. Add two numbers without using arithmetic operators.
  7. Bandy bits in a given number.
  8. Count number of bits to exist flipped to convert a to b .
  9. Find the chemical element that appears in one case.
  10. Notice if two integers have opposite signs.

The bitwise operators should not be used in identify of logical operators. The outcome of logical operators (&&, || and !) is either 0 or one, only bitwise operators return an integer value. Also, the logical operators consider any non-zero operand equally one. For instance, consider the following program, the results of & and && are different for same operands.

C++

#include <iostream>

using namespace std;

int main()

{

int ten = 2, y = 5;

(10 & y) ? cout << "True " : cout << "False " ;

(ten && y) ? cout << "True " : cout << "False " ;

return 0;

}

C

#include <stdio.h>

int principal()

{

int x = 2, y = five;

(x & y) ? printf ( "True " ) : printf ( "False " );

(x && y) ? printf ( "True " ) : printf ( "False " );

return 0;

}

The left-shift and right-shift operators are equivalent to multiplication and division by two respectively. As mentioned in bespeak 1, it works merely if numbers are positive.

C++

#include <iostream>

using namespace std;

int main() {

int 10 = 19;

cout<< "10 << 1 = " << (x << 1) <<endl;

cout<< "x >> one = " << (x >> 1) <<endl;

return 0;

}

C

#include <stdio.h>

int main()

{

int x = xix;

printf ( "x << 1 = %d\n" , x << 1);

printf ( "x >> 1 = %d\n" , x >> ane);

return 0;

}

Output

10 << 1 = 38 10 >> ane = 9

The & operator tin be used to quickly check if a number is odd or even. The value of expression (x & 1) would be non-zero just if ten is odd, otherwise the value would be zero.

C++

#include <iostream>

using namespace std;

int chief() {

int ten = 19 ;

(x & 1) ? cout<< "Odd" : cout<< "Even" ;

render 0;

}

C

#include <stdio.h>

int primary()

{

int x = nineteen;

(x & 1) ? printf ( "Odd" ) : printf ( "Even" );

return 0;

}

The ~ operator should exist used advisedly. The consequence of ~ operator on a modest number can be a big number if the result is stored in an unsigned variable. And the outcome may be a negative number if the result is stored in a signed variable (bold that the negative numbers are stored in 2'due south complement class where the leftmost bit is the sign bit)

C++

#include <iostream>

using namespace std;

int main() {

unsigned int ten = i;

signed int a = 1;

cout<< "Signed Consequence " << ~a <<endl ;

cout<< "Unsigned Issue " << ~x ;

return 0;

}

C

#include <stdio.h>

int primary()

{

unsigned int x = 1;

printf ( "Signed Consequence %d \north" , ~ten);

printf ( "Unsigned Result %ud \n" , ~x);

return 0;

}

Output

Signed Issue -2 Unsigned Result 4294967294
  1. $.25 manipulation (Important tactics)
  2. Bitwise Hacks for Competitive Programming
  3. Scrap Tricks for Competitive Programming

Which Of The Following Commands ​creates An Alternate Data Stream?,

Source: https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/

Posted by: blumandescols.blogspot.com

0 Response to "Which Of The Following Commands ​creates An Alternate Data Stream?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel