Monday 4 April 2011

Inline Functions vs Functions


Inline Functions vs Functions

Functions are pieces of code that when called jump to the address of that code and executes. Inline functions on the other hand are pieces of code that are expanded upon calling it, and then it executes the expanded code. Inline functions are faster than functions, but are only to be used if the inline is small. An inline with 100 line of coding wouldn’t be efficient while an inline with 10 lines would be.

To start an inline it has to outside of the main() like a function. You first define the inline function with “inline” followed by the datatype, the name of the inline, and finally the parameters. It should look something like this:

Inline char toUpperCase(char){

}

Now to test it just code inside of your inline, remember to make the code short and simple.

Inline char toUpperCase(char){
     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a );
}

So we’re going to test this program, a simple program that converts lowercase characters into upper case characters:

01. #include <stdio.h> 
02. 
03. inline char toupper( char a ) {    
04.     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
05. }
06.  
07. int main() {
08.     printf("Enter a character: ");
09.     char ch = toupper( getc(stdin) );
10.     printf( "%c\n\n", ch );
11. } 
In line 03 we define the inline, and in line 04 it will execute the code to change  lowercase to uppercase. In line 09 is where the inline is called. This is what actually happens:

01. #include <stdio.h> 
02. 
03. inline char toupper( char a ) {    
04.     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
05. }
06.  
07. int main() {
08.     printf("Enter a character: ");
09.     char ch = inline char toupper( getc(stdin) ) {    
10.        return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
11.     }
12. }
13.     printf( "%c\n\n", ch );
14. } 

The inline function is expanded here during compile time making the script run faster. Functions are called and go to the address of the function making the program run slower.

*RULE: To make a inline need to use the keyword “INLINE”. Inline functions are only used for small codes, they aren’t meant for huge blocks of code.

The program and help was found at: Microsoft Inline Help




April Fools!

     On Friday April 1st I ended up waking up late at around 8:30. It usually takes me around an hour to get to school, I knew I was going to be late anyway so I took my time. While waiting for my second bus my friend Shavar texts me saying "We have a test in OOP". I looked at my phone in disbelief. I ended up calling Shavar and was serious about this test. I started to panic as I got on my bus. I ended up calling another friend of mine, Darren Butcher. He told me he wasn't coming to class today because he had to organize his tuition and bills for school. After I told him we had a test he was in shock and started getting ready for school. He lives around an hour away at Seneca@Newnham, it was already 9:40am and class starts in 10 minutes.
   
     I got on the bus and started to cram everything into my head in 10 minutes. I was standing up with a textbook in my hand in a bus so full of people I could barely move. I ended up getting to York at 10:10am and ran to class. I sat beside Shavar as we talked about the test. He looked on the wiki page and saw that out professor, Fardad posted that the test was today only at 9:33am. He had a hunch that the test was fake. Fardad said the test will be after the break. I opened up my laptop and started studying. Crammed as much stuff i could within 50 minutes. When we had the break he said for everyone to leave the room so he can place the tests on our table. His facial expression was so serious that we I thought the test was real. Everyone was huddled outside the door and started talking, we didn't know if the test was serious or not. One of my classmates proposed the test was real as Fardad wouldn't print out 60 sheets of paper for a prank. After Fardad finished distributing the tests he set up a stop time on the projector. He let us in the class shortly after that. He warned us about getting 0 on the test if we opened the test early! We had to wait for people to come back from break. One of my classmates came in and announced he just took a taxi and wasted $35 to get here to write the test. Another classmate ran down to the library and printed around 15 pages of notes. Since everyone was here we could start. The countdown clock started. We all flipped our test papers.... and "APRIL FOOLS". Everyone laughed in relief. That was a very stressful morning!

Wednesday 23 March 2011

Bitwise and, or, XOR, not, shift left, shift right

To further understand bitwise operators I tried walking through the binary1.cpp under March 13's class.

CODE:

01. #include <cstdio>
02. using namespace std;
03. int main(void){
04.  char A = 0x53;
05.  char B = 0x9A;
06.  char C;
07.  C = A & B;
08.  printf("%X\n", C);
09.  C = A | B;
10.  printf("%X\n", C);
11.  C = ~A;
12.  printf("%X\n", C);
13.  C = ~B;
14.  printf("%X\n", C);
15.  C = A + (~A+1); // C = A - A;
16.  printf("%X\n", C);
17.  C = A ^ B;
18.  printf("%X\n", C);
19.  C = C ^ B;
20.  printf("%X\n", C);
21.  printf("Shifts.......\n");
22.  C = A << 1;
23.  printf("%X\n", C);
24.  C = B << 1;
25.  printf("%X\n", C);
26.  C = A >> 1;
27.  printf("%X\n", C);
28.  C = B >> 1;
29. printf("%X\n", C);
30.  A = 100;
31.  A = A >> 1;  //  A = A / 2
32.  printf("%d\n", A);
33.  A<<=1;  // A *= 2;
34.  printf("%d\n", A);
35.  return 0;
36. }

The first bitwise operator we run into is bitwise and on line 07.
C = A & B

The hexadecimal code for A and B are the following:

A. 53 = 0101 0011
B. 9A = 1001 1010

The bitwise operator searches through the two variables and determines if the binary code for the variables are the same. For example:
We compare 5 and 9.
A. 5 = 0101
B. 9 = 1001
C = 0001

If the corresponding code are both 1's then 1 is true. If its not the code turns into a 0. So the full bitwise and binary code would be "0001 0010" which will print 12 in line 08!

The next bitewise operator we see is bitwise or. This operator goes through both the variables and checks if either or the binary combination is 1, if either of them is one it returns the 1. If its not a one it returns 0.

The second bitwise operator we run into is bitwise or on line 09.
C = A | B;

A. 53 = 0101 0011
B. 9A = 1001 1010
C = 1101 1011

This will display FFFFFFDB in line 10. It will display the F's because binary hexadecimal has 8 bits, and will fill the rest with 1's.

*RULE: if it starts with 1, it will print 6 F's.

The next bitwise operator we encounter is bitwise NOT " ~ ". This simply reverses the binary code that is given.

A. 53 = 0101 0011
B. 9A = 1001 1010

~A = 1010 1100
~B = 0110 0101

In line 17 we see the next bitwise operator which is bitwise XOR " ^ ".  This operator compares the binary code for 2 variables and does the following:
If "1 ^ 0" and "0 ^ 1" are valid you change the binary code to 1.
If "0 ^ 0" and "1 ^ 1" are invalid you change the binary code to 0.

In line 17 the following happens:

A. 53 = 0101 0011
B. 9A = 1001 1010
C = A ^ B

C = 1100 1001
This will print FFFFFFC9 in line 18.

In line 19 we have an exclusive or where C = C ^ B. If we follow the rules from before the answer would be as follows:

C = 1100 1001
B = 1001 1010
NEW C = 0101 0011

This will display 53!

After line 20 we come across shifting. The first shift we encounter is a left shift.
In line 21 we have the following code:

C = A << 1;

The current binary code for C is:

C = 0101 0011
By shifting everything to the left we get:
C = 1010 0110

Which will print : FFFFFFA6

In line 25 we have another left shift.

C = B << 1;
C = 0011 0100

This will print out: 34

When shifting to the left the last number will always be 0.

Now we have shifting to the right in line 26.

C = A >> 1;
You basically just shift everything to the right like you would do when shifting to the left
A =  0101 0011
C =  0010 1001

This will print:  29

In line 28 we have another right shift:

C = B >> 1;
B = 1001 1010
C = 1100 1101

This will print: FFFFFFCD

*RULE: When shifting to the right and the first bit is 1, the bits before it are ALL 1. So when you shift it to the right you shift 1's instead of 0's.

In line 31 you we see A = A >> 1; This is basically the same thing as saying A = A / 2;


A = 100
A = 100 / 2

Making the value of A into : 50

In line 33 we see A << =1; This is the same thing as saying A * = 2;

Making the value of A into 100.

Binary is pretty easy if you get the hang of it!