[Using codeblocks20.03 to realize C language bit operation operation]

foreword

If you feel the position, you are the right match
(= ̄ρ ̄=) …zzZZ

1. How to use codeblocks20.03 to import files

1: Import the .h file:

I found an online tutorial (personal feeling is relatively rough): https://blog.csdn.net/u012822181/article/details/79301403
The following are my own steps:
first find the location of my .h file (my . Both h and .a are on the desktop):
insert image description here
Then enter codeblocks20.03, click Settings->Compiler->Global compiler settings->Search directories->Compiler at the top of the CodeBlocks interface,
insert image description here
click add and then click the icon of the folder style, select Go to the location of the .h file (mine is on the desktop)

and you can see that there is no .h file displayed, which is normal (don’t ask why, I don’t know the specific reason╭∩╮( ̄▽ ̄)╭∩╮ )
and then click to select a folder in the lower right corner, and then click ok, you can see the following interface
insert image description here
Even if it is successful, click ok again, it's done!
This method is the step of adding the .h file (header file), without detailed location (the file is directly selected on the desktop and it is ok)
(note that it is all a personal practice, it can only be used as a reference!!!!)

2: The steps to insert the .h file are as follows:

Enter codeblocks20.03, click Settings->Compiler->Global compiler settings->Linker settings
insert image description here
at the top of the CodeBlocks interface, click add in the lower left corner, click the folder-
insert image description here
style icon , select the . imported
insert image description here

insert image description here

2. Some example codes

1: extract the specified byte n from x

The code is as follows (example):

int get_byte(int x, int n) {
    
    
	int	data=((x >>  n*8) & 0x000000ff);
	printf("0x%x\n",data);
}

2: Logically shift the signed integer x to the right by n bits

The code is as follows (example):

int logical_shift(int x, int n) {
    
    
    int data = (unsigned int)x >> n;
    printf("0x%x\n", data);
}

3: The number of 1s in the binary representation of x

The code is as follows (example):

int bit_cnt(int x) {
    
    
	int count = 0;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
    
    
		if (((x>>i) & 1) == 1)
		{
    
    
			count++;
		}
	}
	printf("%d\n",count);
}

4: Do not use the ! operator to implement !x

int nnot(int x) {
    
    
	int data= (x&&1)^1;
	printf("%d\n",data);
}

5: Minimum value of 32-bit int

int tmin(void) {
    
    
	int i=0;
	i=~i;
	i=i<<(sizeof(int)*8-1);
	printf("%d\n",i);
}

6: Can n-bit two's complement represent x? Can return 1, otherwise return 0

int fits_bits(int x, int n) {
    
     //n位能否表示数字x (5,3) (-4,3)
if(x>=0){
    
    
 if ( x>((1<<(n-1))-1) )
 	printf("0\n"); 
	else
	printf("1\n");
}
 
else{
    
    
	if(x<(-1)<<(n-1))
		printf("0\n");
	else
		printf("1\n");
	}
}

7: return -x

int neg(int x) {
    
    
	int data;
	if(x>=0)
	{
    
    
		data=(~x)+1;
		printf("%d\n",data);
	}
		if(x<0)
	{
    
    
		data=~(x-1);
		printf("%d\n",data);
	}
}

8: Return 1 if x > 0, otherwise return 0

int is_positive(int x) {
    
    
	if(x==0){
    
    
		printf("0\n");
		return 0;
	}
	
	if(x>>31==0){
    
    
		printf("1\n") ;	
	}
	else{
    
    
	printf("0\n") ;	
	}
}

9: If x <= y return 1, otherwise return 0

int is_le(int x, int y) {
    
    	
  int res = x + (~y + 1);
  int flag1 = x & (~y);
  int flag2 = (~(x ^ y)) & res;
  int flag3 = ((x^y)==0)<<31;
  printf( "%d\n",(flag1 | flag2 |flag3) >> 31 & 1);	
}

Summarize.

The road is long and long, I will search up and down
(✧\ ٩(눈౪눈)و //✧Come on, hello)

Guess you like

Origin blog.csdn.net/qq_57903239/article/details/125119489