In this class, I learn the bitwise shift operator including '<<' and '>>'.


1. Right-shift operator (>>)
    - The right-shift operator (<<
) moves the bits of an integer or enumeration type expression to the right.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b1000;
    
    A = A >> 2;
    
    printf('%x', A);    
    
    return 0;
}

The results showed 'A' as 0b0010 that which resulted in change of 2^(-2).



2. Left-shift operator (<<) 
    - The left-shift operator (<<) moves the bits to the left.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b0001;
    
    A = A << 2;
    
    printf('%x', A);    
    
    return 0;
}

The results showed 'A' as 0b0100 that which changed by the square of 2.

In this class, I learned about the logical operators (e.g. &&, ||, !) and bit operators (&, |, ~, ^).

1. Logical operators
    - AND (&&): If A and B are true, it is denoted as 'A && B'
    - OR (||): If A or B are true, it is denoted as 'A || B'
    - NOT (!): Non-A values output as a result, it is denoted as '!A'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 1, B = 0, C = 1;
    int Result1, Result2, Result3, Result4, Result5, Result6
    
    Result1 = A && B;
    Result2 = A && C;
    Result3 = A || B;
    Result4 = B || C;
    Result5 = !A;
    Result6 = !B;
    
    print('%d \n', Result1);
    print('%d \n', Result2);
    print('%d \n', Result3);
    print('%d \n', Result4);
    print('%d \n', Result5);
    print('%d', Result6);
    
    return 0;
}


2. Bit operators
    - AND (&): Calculate each bit for AND
    - OR (|): Calculate each bit for OR
    - NOT (~): Calculate each bit for NOT

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int A = 0b1100, B = 0b1010;
    int Result1, Result2, Result3;
    
    Result1 = A & B;
    Result2 = A | B;
    Result3 = ~A;
    
    printf('%x \n', Result1);
    printf('%x \n', Result2);
    printf('%x', Result3);
        
    return 0;
}

 

In this class, I learned about the operators that enable calculate or compare the variables.
Here's four types of operators that easily classified to explan their functions.

1. Assignment operator
    - 'A = B' means that value 'B' should be assigned to variable 'A'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    a = 123;
    printf('%d', a);
    
    return 0;
}

 

 

2. Relational operator
    - Comparing the relationship between left and the right and results showed whether the operator is true or false.
    - Examples: 'A > B', 'A >= B', 'A! = B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    int b;
    
    a = 123;
    b = 456;
    
    printf('%d \n', a > b);
    printf('%d \n', a >= b);
    printf('%d \n', a == b);
    printf('%d \n', a != b);
    
    return 0;
}

 

3. Arithmetic operator
    - Examples: 'A + B', 'A - B', 'A * B', 'A / B', 'A % B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 123 + 1;
    printf('%d \n', a);
    
    a = 123 - 2;
    printf('%d \n', a);
    
    a = 123 * 3;
    printf('%d \n', a);
    
    a = 123 / 10;
    printf('%d \n', a);
    
    a = 123 % 7;
    printf('%d \n', a);
    
    return 0;
}

 

4. Incremental operator
    - Examples: 'A ++' means 'A = A + 1 and 'A --' means 'A = A - 1'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 1;
    printf('%d \n', a);
    
    a++;
    pritnf('%d \n', a);
    
    a--;
    printf('%d \n', a);
    
    return 0;
}

 

5. Compound substitution operator
    - Examples: 'A += B' means 'A = A + B' and 'A -= B' means 'A = A - B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 2;
    printf('%d \n', a);
    
    a += 2;
    printf('%d \n', a);
    
    return 0;
}

In this class, I learned about quadratic expression for specific values.
Computers utilize binary, octal, and hexadecimal numbers, and their purpose of use varies depending on the pros and cons of the expression method.

1) Binary expression: 0 or 1
2) Octal expression: 0 ~ 7
3) Hexadecimal expression
     - 0 ~ 9 and A ~ F are used
     - Using %X for printing the value with printf() function

And here's a example code for converting from decimal to binary, octal and hexadecimal expression. 

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    printf("%d \n", 123);
    
    printF("%d \n", 0b01111011);   // '0b' means binary expression
    
    printf("%o", 123);
    
    printf("%x", 123);
    
    return 0;
}

 

In this class, I learned about how to represent the letter in the C language.
ASCII code is a representation method for expressing the letter with a form of number.
For example, capital "A" is exchanged as 65, and "a" is exchanged as 97.
Therefore, if you write a code as "printf("%c", a)", computer input a number 97, not a letter 'a'.

To find a specific letter, look for the letters in the link below.
https://www.ascii-code.com/

 

Here's a example code for understanding how the ASCII code is running in the C programming.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    printf("%d", a);
    
    return 0;
}

Results showed that letter "a" has a 97 that denoted as the ASCII code.

+ Recent posts