From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONCEPT/bit_shift.html | 219 +++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 reference/C/CONCEPT/bit_shift.html (limited to 'reference/C/CONCEPT/bit_shift.html') diff --git a/reference/C/CONCEPT/bit_shift.html b/reference/C/CONCEPT/bit_shift.html new file mode 100644 index 0000000..507400c --- /dev/null +++ b/reference/C/CONCEPT/bit_shift.html @@ -0,0 +1,219 @@ +Bitwise operations + +
+
+

Bitwise operations

+
+
+ +

+Bitwise operators include: +

+

+ + + + + + + + + + + +
& AND
&= AND
| OR
|= OR
^ XOR
~ one's compliment
<< Shift Left
>> Shift Right
<<= Shift Left
>>=Shift Right
+
+

+ + +


+

AND OR and XOR

+ +These require two operands and will perform bit comparisions. +

+AND & +will copy a bit to the result if it exists in both operands. +

+

+ + +
+
+  
+  main()
+  {
+    unsigned int a = 60;	/* 60 = 0011 1100 */  
+    unsigned int b = 13;	/* 13 = 0000 1101 */
+    unsigned int c = 0;           
+
+    c = a & b;                  /* 12 = 0000 1100 */ 
+  }
+
+
+
+
+

+OR | will copy a bit if it exists in eather operand. +

+

+ + +
+
+  
+  main()
+  {
+    unsigned int a = 60;	/* 60 = 0011 1100 */  
+    unsigned int b = 13;	/* 13 = 0000 1101 */
+    unsigned int c = 0;           
+
+    c = a | b;                  /* 61 = 0011 1101 */ 
+  }
+
+
+
+
+

+XOR ^ copies the bit if it is set in one operand (but not both). +

+

+ + +
+
+  
+  main()
+  {
+    unsigned int a = 60;	/* 60 = 0011 1100 */  
+    unsigned int b = 13;	/* 13 = 0000 1101 */
+    unsigned int c = 0;           
+
+    c = a ^ b;                  /* 49 = 0011 0001 */ 
+  }
+
+
+
+
+ +

+ +


+

Ones Complement

+ +This operator is unary (requires one operand) and has the efect of +'flipping' bits. +

+

+ + +
+
+
+  main()
+  {
+    unsigned int Value=4;          /*   4 = 0000 0100 */  
+
+    Value = ~ Value;               /* 251 = 1111 1011 */  
+
+  }
+
+
+
+
+

+ + +

+ +


+

Bit shift.

+The following
operators +can be used for shifting bits left or right. +

+

+ + + + + + + +
<< >> <<= >>=
+
+

+The left operands value is moved left or right by the number of bits specified +by the right operand. For example: +

+

+ + +
+
+
+  main()
+  {
+    unsigned int Value=4;          /*  4 = 0000 0100 */  
+    unsigned int Shift=2;
+
+    Value = Value << Shift;        /* 16 = 0001 0000 */  
+
+    Value <<= Shift;               /* 64 = 0100 0000 */  
+
+    printf("%d\n", Value);         /* Prints 64      */  
+  }
+
+
+
+
+

+Usually, the resulting 'empty' +bit is assigned ZERO. +Please use +unsigned +variables with these operators to avoid unpredictable +results. +

+


+

Examples:

+ AND
+ OR
+ Bit shifting. +
+

Problem:

+ +Bit shifting problem. +
+

See Also:

+ +All the other Expressions and operators.
+ +Operator precedence.
+ +Assignment Operators. +
+

+

+ + + + +
+Top + +Master Index + +Keywords + +Functions +
+
+

+


+
Martin Leslie + +
+ -- cgit v1.2.3-54-g00ecf