Difference between revisions of "OOP344-Jason Quan C/C++ Programs & notes-20102"
(added bitwise operators set and unset bits to notes) |
(fixed some of tab spaces in my notes) |
||
Line 113: | Line 113: | ||
#define mask 32 | #define mask 32 | ||
void printdata(int v, int m){ | void printdata(int v, int m){ | ||
− | + | for(m;m>0;m = m >>1){ | |
− | + | printf("%d",!!(v & m)); | |
− | + | } | |
− | + | printf("\n"); | |
} | } | ||
/*set bit to one or zero and output the binary*/ | /*set bit to one or zero and output the binary*/ | ||
void set(int* v, int num){ | void set(int* v, int num){ | ||
− | + | *v=((*v)^num); /*0^0=0 and 1^0=1* therefore the operator will set the bit to 1 or 0*/ | |
− | + | printdata(*v,mask); | |
} | } | ||
int main(){ | int main(){ | ||
− | + | int a; | |
− | + | int m; | |
− | + | do{ | |
− | + | printf(" enter number(number>32 to quit):\n"); | |
− | + | scanf("%d",&a); | |
− | + | if(a<32){ | |
− | + | printdata(a,mask); | |
− | + | printf("the current number is: %d\n",a); | |
− | + | printf("set bit:\n"); | |
− | + | scanf("%d",&m); | |
− | + | set(&a,m); | |
− | + | printf("The new number: %d\n", a); | |
− | + | printf("unset bit:\n"); | |
− | + | scanf("%d",&m); | |
− | + | set(&a,m); | |
− | + | printf("The new Number: %d\n", a); | |
} | } | ||
− | + | }while(a<32); | |
} | } | ||
</pre> | </pre> |
Latest revision as of 22:25, 28 June 2010
pointer to functions
A pointer can be used to point to anything. Therefore you can also use it to point to a function, here’s an simple c program that uses pointer to function:
#include<stdio.h> void windows(char,int); void mac(char,int); int main(void) { void (*platform)(char,int); /* this a declaration of a function pointer with a argument of char and int*/ char name[30]; int option; printf("Please enter your name: "); scanf("%s", &name); do{ printf("Which platform 1.Windows, 2.Mac:"); scanf("%d", &option); platform = (option < 2) ? windows : mac; /* this line assign where the platform pointer should point to.*/ (option > 2||option<1) && printf("error: pick 1 or 2\n"); /*Lazy Evaculation*/ }while(option<1||option>2); platform(name , option); return 0; } void windows(char* str,int n) { printf("%s you've choosen option %d, you are a Windows user.\n",str,n); } void mac(char* str,int m) { printf(" %s you've chosen Option %d, you are Mac user.\n",str, m); }
In this program two functions are declared: void Windows(char *str,int n) and void Mac(char *str ,int m) they both return void. But the output a line. Within the main() a void pointer call platform is declared with two arugments( char, int) which are values types that will be pasted to the corrsponding functions which is determine by this line : platform = (option < 2) ? windows : mac;
If the user chooses option 1 platform will be set to point to the windows function else Mac fun ction otherwise. To call the function, you would call it using the pointer platform by use a line: platform(name , option); this line will call the function.
decimal to binary with bitwise operators
here's a example of dec to binary using bitwise operator
#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; }
the Mask: The length of the mask can be determine by an simple intger for example 30= 100000 64 =1000000 1024= 10000000000
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.
set and unset bits
While I was studying for the OOP344 test, I created a small program that demonstrates how to set a bit or unset a bit. For example if the number is 1 and the binary is 000001. Therefore if I choose to set the bit 2 the binary will become 000011 and the number will become 3. However if I choose to unset 1 the binary will become 000010. Thus a set and unset bit function has been created.
#include <stdio.h> #define mask 32 void printdata(int v, int m){ for(m;m>0;m = m >>1){ printf("%d",!!(v & m)); } printf("\n"); } /*set bit to one or zero and output the binary*/ void set(int* v, int num){ *v=((*v)^num); /*0^0=0 and 1^0=1* therefore the operator will set the bit to 1 or 0*/ printdata(*v,mask); } int main(){ int a; int m; do{ printf(" enter number(number>32 to quit):\n"); scanf("%d",&a); if(a<32){ printdata(a,mask); printf("the current number is: %d\n",a); printf("set bit:\n"); scanf("%d",&m); set(&a,m); printf("The new number: %d\n", a); printf("unset bit:\n"); scanf("%d",&m); set(&a,m); printf("The new Number: %d\n", a); } }while(a<32); }