Changes

Jump to: navigation, search

OOP344-Jason Quan C/C++ Programs & notes-20102

924 bytes added, 22:11, 20 June 2010
added dec to binary using bitwise operator to my notes page
platform(name , option);
this line will call the function.</p>
 
==decimal to binary with bitwise operators==
 
<p> here's a example of dec to binary using bitwise operator</p>
<pre>
 
#include <stdio.h>
 
void binary(int v, int mask){
 
for(mask;mask>0;mask = mask >>1){
 
printf(“%d”,!!(v & mask)); /*prints 1 or 0, since bitwise operator “&” means: 1 & 1= 1 anything else =0.*/
 
}
 
}
 
int main(){
 
int a;
 
int m;
 
printf(“number:”);
 
scanf(“%d”,&a);
 
m=a>32? 1024:32;
 
binary(a,m);
 
printf(“\n”);
 
return 0;
 
}
 
</pre>
 
<p>
 
the Mask:
 
The length of the mask can be determine by an simple intger for example
 
30= 100000
 
64 =1000000
 
1024= 10000000000
</p>
 
<p>
The number:
 
the number is an plain integer number. But when used with an bitwise operator it become a binary number such as:
 
1 & mask
 
1= 000001
 
mask= 100000
 
Therefore in the loop shifts the mask right 1 to determine if the position contains 1 or 0. If zero output prints zero else one.
 
</p>
1
edit

Navigation menu