search

Does Linux C language have bits?

Mar 28, 2023 am 10:57 AM
linuxc languagebit

Linux C language has bit; in the microcontroller C language, bit is a new keyword, often used to define a "bit variable"; the method of defining bit type data in C language is: 1. Through sbit Or bit definition; 2. Define through bit field (in structure); 3. Define through combined bit operators.

Does Linux C language have bits?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Does Linux C language have bits?

have.

C language defines bit type data:

1. Definition through sbit or bit

sbit is mapped to the IO port (IO such as P1^1 The "bit" of the port)

bit is in the bit-addressable space in RAM and is generally used as a flag bit for program judgment.

Consider that one of them is external (sbit) and the other is internal (bit).

In the microcontroller C language, bit is a new keyword, often used to define a "bit variable"

2. Define through bit fields (in structures)

Definition of bit fields and description of bit field variables Bit field definition is similar to structure definition, and its form is:

 struct bit field structure name

 {Bit field list};

The form of the bit field list is: Type specifier bit domain name: bit field length

For example:

struct bs 
{ 
 int a:8; 
 int b:2; 
 int c:6; 
};

The description of bit field variables is the same as the description of structure variables. You can define first and then explain, define and explain at the same time, or explain directly. For example:

struct bs 
{ 
 int a:8; 
 int b:2; 
 int c:6; 
}data;

It indicates that data is a bs variable, occupying two bytes in total. Bit field a occupies 8 bits, bit field b occupies 2 bits, and bit field c occupies 6 bits. There are several explanations for the definition of bit fields:

1. A bit field must be stored in the same byte and cannot span two bytes. If the remaining space of one byte is not enough to store another bit field, the bit field should be stored from the next unit. You can also intentionally start a bit field from the next unit. For example:

struct bs 
{ 
 unsigned a:4 
 unsigned :0 /*空域*/ 
 unsigned b:4 /*从下一单元开始存放*/ 
 unsigned c:4 
}

In this bit field definition, a occupies 4 bits of the first byte, fill in the last 4 bits with 0 to indicate not used, b starts from the second byte, occupying 4 bits, and c occupies 4 bits Bit.

 2. Since the bit field is not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is, it cannot exceed 8 binary bits.

 3. The bit field can have no bit domain name. At this time, it is only used for filling or adjusting the position. Unnamed bit fields cannot be used. For example:

struct k 
{ 
 int a:1 
 int :2 /*该2位不能使用*/ 
 int b:3 
 int c:2 
};

From the above analysis, it can be seen that bit field is essentially a structure type, but its members are allocated in binary terms.

2. The use of bit fields

The use of bit fields is the same as the use of structure members. Its general form is: Bit field variable name • Bit domain name Bit fields allow output in various formats.

main(){ 
 struct bs 
 { 
  unsigned a:1; 
  unsigned b:3; 
  unsigned c:4; 
 } bit,*PBit; 
 bit.a=1; 
 bit.b=7; 
 bit.c=15; 
 printf("%d,%d,%d\n",bit.a,bit.b,bit.c); 
 PBit=&bit; 
 PBit->a=0; 
 PBit->b&=3; 
 PBit->c|=1; 
 printf("%d,%d,%d\n",PBit->a,PBit->b,PBit->c); 
}

The above example program defines the bit field structure bs, and the three bit fields are a, b, and c. The variable bit of type bs and the pointer variable PBit pointing to type bs are explained. This means that bitfields can also use pointers.

Lines 9, 10, and 11 of the program assign values ​​to three bit fields respectively. (It should be noted that the assignment cannot exceed the allowable range of the bit field) Line 12 of the program outputs the contents of the three fields in integer format. Line 13 sends the address of the bit field variable bit to the pointer variable PBit. Line 14 uses a pointer to reassign bit field a to 0. Line 15 uses the compound bit operator "&=". This line is equivalent to: PBit->b=PBit->b&3. The original value in bit field b is 7, and the result of the bitwise AND operation with 3 is 3 (111&011=011, decimal value is 3). Similarly, the compound bit operation "|=" is used in line 16 of the program, which is equivalent to: PBit->c=PBit->c|1 and the result is 15. Line 17 of the program outputs the values ​​of these three fields using pointers.

The main purpose of using bit fields is to compress storage. The general rules are:

1) If the types of adjacent bit fields are the same and the sum of their bit widths is less than the sizeof size of the type , then the following field will be stored immediately next to the previous field until it cannot be accommodated;

2) If the types of adjacent bit field fields are the same, but the sum of their bit widths is greater than the sizeof size of the type, then the following field The field will start from a new storage unit with an offset that is an integer multiple of its type size;

3) If bitfield fields are interspersed with non-bitfield fields, no compression will be performed;

4) The total size of the entire structure is an integer multiple of the size of the widest basic type member.

3. Operate by combining bit operators

#define Setbit(x,y)  (x|=(0x01<<y))   //置位
#define Clrbit(x,y)  (x&=(~(0x01<<y)))  //复位(清零)
#define Chkbit(x,y)  (x&(0x01<<y))     //检位

Recommended learning: "linux video tutorial"

The above is the detailed content of Does Linux C language have bits?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools