💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Lecture PPT
  • Online Lesson
  • Bitwise Operation in C
  • Set flag: (플래그 |= 마스크)
  • Clear flag (플래그 &= ~마스크)
  • Toggle flag (플래그 ^= 마스크)
  • Read a bit
  • Read multiple bits
  • Tip: Use Macro
  • Exercise
  • Exercise 1
  • Exercise 2
  • Exercise 3

Was this helpful?

  1. C Programming
  2. C-Programming Lessons

Bitwise Operation

PreviousDynamic AllocNextSyllabus

Last updated 8 months ago

Was this helpful?

Lecture PPT

Online Lesson

코딩도장 핵심요약:

Bitwise Operation in C

자료형과 메모리 주소를 바이트 단위로 구분하여 사용하였습니다. 비트 연산자는 바이트 단위보다 더 작은 비트 단위로 연산하는 연산자입니다.

Example

	PA=PA | (1<<5);  	// set PA5 (as High) and mask others
	PA=PA & ~(1<<5);  	// reset PA5 (as LOW) and mask others
	bit = PA & (1<<5);  // check the bit5. bit=1 if  PA5 is 1


Set flag: (플래그 |= 마스크)

a |= (1 << k)

Clear flag (플래그 &= ~마스크)

Example:

unsigned char flag = 7;    // 7: 0000 0111
flag &= ~2;    

0000 0010
_________ ~
1111 1101

Toggle flag (플래그 ^= 마스크)

a ^= 1<<k

Read a bit

(Method 1) bit = a & (1<<k) // Shift ‘bit 1’ left by k starting from LSB

(Method 2) bit = (a >>k) & (1) // Shift target ‘bit right by k

Example:

Read multiple bits

Tip: Use Macro

#define BIT_SET(REG, BIT)		((REG) |= 1<< (BIT))
#define BIT_CLEAR(REG, BIT)		((REG) &= ~1<<(BIT))
#define BIT_READ(REG, BIT)		((REG)>>BIT & (1))
#define BITS_CLEAR(REG, BIT,NUM)	((REG) &= ~((0x1<< NUM)-1)<<(BIT))
#define BITS_SET(REG, BIT,NUM)		((REG) |= NUM<< (BIT))


Exercise

Exercise 1

What will be the output ?

#include <stdio.h>
 
int main()
{
    unsigned char num1 = 1;    // 0000 0001
    unsigned char num2 = 3;    // 0000 0011
    unsigned char num3 = 162;    // 162: 1010 0010
    unsigned char num4;
    num4 = ~num3;

 
    printf("%d\n", num1 & num2);    
    printf("%d\n", num1 | num2);    
    printf("%d\n", num1 ^ num2);    
    printf("%u\n", num4);    // 93: 0101 1101: num1의 비트 값을 뒤집음
 
    return 0;
}
#include <stdio.h>
 
int main()
{
    unsigned char num1 = 1;    // 0000 0001
    unsigned char num2 = 3;    // 0000 0011
    unsigned char num3 = 162;  // 162: 1010 0010
    unsigned char num4;
    num4 = ~num3;

    printf("%d\n", num1 & num2);    // 0000 0001: 01과 11을 비트 AND하면 01이 됨
    printf("%d\n", num1 | num2);    // 0000 0011: 01과 11을 비트 OR하면 11이 됨
    printf("%d\n", num1 ^ num2);    // 0000 0010: 01과 11을 비트 XOR하면 10이 됨
    printf("%u\n", num4);    // 93: 0101 1101: num1의 비트 값을 뒤집음
 
    return 0;
}

Exercise 2

What will be the output?

#include <stdio.h> 

int main()
{
    unsigned char num1 = 4;    // 4: 0000 0100
    unsigned char num2 = 4;    // 4: 0000 0100
    unsigned char num3 = 4;    // 4: 0000 0100
    unsigned char num4 = 4;    // 4: 0000 0100
    unsigned char num5 = 4;    // 4: 0000 0100
 
    num1 &= 5;     // 5(0000 0101) AND 연산 후 할당
    num2 |= 2;     // 2(0000 0010) OR 연산 후 할당
    num3 ^= 3;     // 3(0000 0011) XOR 연산 후 할당
    num4 <<= 2;    // 비트를 왼쪽으로 2번 이동한 후 할당
    num5 >>= 2;    // 비트를 오른쪽으로 2번 이동한 후 할당
 
    printf("%u\n", num1);    
    printf("%u\n", num2);    
    printf("%u\n", num3);    
    printf("%u\n", num4);    
    printf("%u\n", num5);    
 
    return 0;
}
#include <stdio.h> 

int main()
{
    unsigned char num1 = 4;    // 4: 0000 0100
    unsigned char num2 = 4;    // 4: 0000 0100
    unsigned char num3 = 4;    // 4: 0000 0100
    unsigned char num4 = 4;    // 4: 0000 0100
    unsigned char num5 = 4;    // 4: 0000 0100
 
    num1 &= 5;     // 5(0000 0101) AND 연산 후 할당
    num2 |= 2;     // 2(0000 0010) OR 연산 후 할당
    num3 ^= 3;     // 3(0000 0011) XOR 연산 후 할당
    num4 <<= 2;    // 비트를 왼쪽으로 2번 이동한 후 할당
    num5 >>= 2;    // 비트를 오른쪽으로 2번 이동한 후 할당
 
    printf("%u\n", num1);    //  4: 0000 0100: 100과 101을 비트 AND하면 100이 됨
    printf("%u\n", num2);    //  6: 0000 0110: 100과 010을 비트 OR하면 110이 됨
    printf("%u\n", num3);    //  7: 0000 0111: 100과 011을 비트 XOR하면 111이 됨
    printf("%u\n", num4);    // 16: 0001 0000: 100을 왼쪽으로 2번 이동하면 10000이 됨
    printf("%u\n", num5);    //  1: 0000 0001: 100을 오른쪽으로 2번 이동하면 1이 됨
 
    return 0;
}

Exercise 3

Download and Read instruction in the given source file.

Fill in the blanks.

#include <stdio.h>
#include <stdint.h>

void dec2bin(unsigned int n);

void main() {
    
	uint8_t vals=0;
	
	// Assume 8 LEDs are connected to Digital Out pins of Port A (PA)
	// LED0 at PA[0] to LED7 at PA[7]
	uint8_t PA;
	
	// Initial Values of PA
	PA = 0b00001111;			
	printf("\n Initial PA: ");
	dec2bin(PA);
	
	
	// Exercise_1-1: Turning ON LED4 at PA[4] 
	PA |=  ________; 	    // turn ON LED4 
	printf("\n ex1-1: ");
	dec2bin(PA);
	
	//Exercise_1-2:  Read the bit at PA[4], 4th from LSB
	vals =  ________;	// read bit of PA[4]
	printf("\n ex1-2: %d\n", vals);
	
	
	// Exercise_2-1: Turn off LED4 at PA[4]
	PA &= ________;    	// turn off LED4  
	printf("\n ex2-1: ");
	dec2bin(PA);
	
	//Exercise_2-2:  Read the bit at PA[4], 4th from LSB
	vals =  ________;	// read bit of PA[4]
	printf("\n ex2-2: %d\n", vals);
	
	
	
	// Exercise_3-1: Turning ON LED5 and LED4,  at PA[5:4] 
	PA |=  ________;	// turn ON LED5 and LED4
	printf("\n ex3-1: ");
	dec2bin(PA);
	
	//Exercise_3-2:  Read bits LED5 and LED4,  at PA[5:4] 
	vals =  ________;	// read bits at PA[5:4] 
	printf("\n ex3-2: %d\n", vals);
	
	//Exercise_4-1:  SET  PA[0] and PA[7] 
	PA |= 0b10000001; 
	printf("\n ex4-1: ");
	dec2bin(PA);
	
	//Exercise_4-2:  CLEAR  PA[0] and PA[7] 
	PA &=  ________;
	printf("\n ex4-2: ");
	dec2bin(PA);
	
	
	//Exercise_5-1:  Toggle LED7 PA[7]
	printf("\n");    
	PA ^=  ________;
	printf("\n ex5-1: ");
	dec2bin(PA);
	
	printf("\n");    
	
}

void dec2bin(unsigned int n) {
	unsigned int a = 0x80;
	for (int i = 0; i < 8; i++) {
		if ((a & n) == a)
			printf("1");
		else
			printf("0");
		a = a >> 1;
	}
}
 PA   : 00001111
 ex1-1: 00011111
 ex1-2: 1

 ex2-1: 00001111
 ex2-2: 0

 ex3-1: 00111111
 ex3-2: 3

 ex4-1: 10111111
 ex4-2: 00111110

 ex5-1: 10111110

C_bitwise_exercise_01.c
C_bitwise_exercise_02.c
C_bitwise_exercise_03.c
비트연산자 사용하기 핵심요약
482KB
Tutorial_C_Bitwise_2023.pdf
pdf