본문 바로가기

Study Meeting/빡공단 30기

[Day 9] Operator 2: Logical Operators and Bit operators in C Language

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;
}