summaryrefslogtreecommitdiff
path: root/reference/C/CONCEPT/bit_shift.html
blob: 507400c91f29f40d0b85d2b9c5a465ba5eda92cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<title>Bitwise operations</title>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Bitwise operations</h1>
</center>
<hr>

<p>
Bitwise operators include:
<p>
<center>
<table border=2 width=40% bgcolor=ivory>
<tr align=center><td>&amp;</td>     <td><a href="#aox">AND</a></td></tr>
<tr align=center><td>&amp;=</td>    <td><a href="#aox">AND</a></td></tr>
<tr align=center><td>|</td>         <td><a href="#aox">OR</a></td></tr>
<tr align=center><td>|=</td>        <td><a href="#aox">OR</a></td></tr>
<tr align=center><td>^</td>         <td><a href="#aox">XOR</a></td></tr>
<tr align=center><td>~</td>         <td><a href="#one">one's compliment</a></td></tr>
<tr align=center><td>&lt;&lt;</td>  <td><a href="#shift">Shift Left</a></td></tr>
<tr align=center><td>&gt;&gt;</td>  <td><a href="#shift">Shift Right</a></td></tr>
<tr align=center align=center><td>&lt;&lt;=</td> <td><a href="#shift">Shift Left</a></td></tr>
<tr align=center><td>&gt;&gt;=</td><td><a href="#shift">Shift Right</a></td></tr>
</table>
</center>
<p>

<a name=aox>
<hr>
<h2>AND OR and XOR</h2>

These require two operands and will perform bit comparisions.
<p>
<b>AND &</b>
will copy a bit to the result if it exists in both operands.
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
  
  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 */ 
  }

</pre>
</td></tr>
</table>
</center>
<p>
<b>OR |</b> will copy a bit if it exists in eather operand.
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
  
  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 */ 
  }

</pre>
</td></tr>
</table>
</center>
<p>
<b>XOR ^</b> copies the bit if it is set in one operand (but not both).
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>
  
  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 */ 
  }

</pre>
</td></tr>
</table>
</center>

<p>
<a name=one>
<hr>
<h2>Ones Complement</h2>

This operator is unary (requires one operand) and has the efect of 
'flipping' bits. 
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>

  main()
  {
    unsigned int Value=4;          /*   4 = 0000 0100 */  

    Value = ~ Value;               /* 251 = 1111 1011 */  

  }

</pre>
</td></tr>
</table>
</center>
<p>


<p>
<a name=shift>
<hr>
<h2>Bit shift.</h2>
The following <a href="../CONCEPT/expressions.html">operators</a>
can be used for shifting bits left or right. 
<p>
<center>
<table border=2 width=50% bgcolor=ivory>
<tr align=center>
<td width=25%> &lt;&lt; </td>
<td width=25%> &gt;&gt; </td>
<td width=25%> &lt;&lt;= </td>
<td width=25%> &gt;&gt;= </td>
</tr>
</table>
</center>
<p>
The left operands value is moved left or right by the number of bits specified
by the right operand. For example:
<p>
<center>
<table border=2 bgcolor=ivory>
<tr><td>
<pre>

  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      */  
  }

</pre>
</td></tr>
</table>
</center>
<p>
Usually, the resulting 'empty' 
bit is assigned ZERO.
Please use 
<a href="data_types.html#modifier">unsigned</a> 
variables with these operators to avoid unpredictable
results.
<p>
<hr>
<h2>Examples:</h2>
<a href="../EXAMPLES/and.c"><img src=../../GRAPHICS/computer.gif></a> AND<br>
<a href="../EXAMPLES/or.c"><img src=../../GRAPHICS/computer.gif></a> OR<br>
<a href="../EXAMPLES/bit_shift.c"><img src=../../GRAPHICS/computer.gif></a> Bit shifting.
<hr>
<h2>Problem:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../PROBLEMS/problems.html#binary">Bit shifting problem.</a>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
All the other <a href="expressions.html#bit">Expressions and operators</a>.<br>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../CONCEPT/precedence.html">Operator precedence.</a><br>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../CONCEPT/assignment.html">Assignment Operators</a>.
<hr>
<p>
<center>
<table border=2 width=80% bgcolor=ivory>
<tr align=center>
<td width=25%>
<a href="../cref.html">Top</a>
</td><td width=25%>
<a href="../master_index.html">Master Index</a>
</td><td width=25%>
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address>