- Test_bit macro in C/C++
- [Solved]-What’s the meaning of BIT() in this linux kernel macro?-C
- Related Query
- More Query from same tag
- Bit macro in linux
- Some important macros in the kernel
- Linux system kernel and kernel modules
- Operating system: why to distinguish between user mode and kernel mode
- How to distinguish the root under the Linux system?
Test_bit macro in C/C++
I’m trying to read input of a linux device via ioctl() and I saw on many example of code with the «test_bit» macro but the only code I found about it is this : #define test_bit(bit, array) (array[bit / 8] & (1
void test()< uint8_t key_b[KEY_MAX/8 + 1]; /* the events (up to 64 at once) */ const char *keyboard = "/dev/input/keyboard0"; int keybrdToCapture; int yalv; keybrdToCapture = open(keyboard, O_RDONLY); memset(key_b, 0, sizeof(key_b)); ioctl(keybrdToCapture, EVIOCGKEY(sizeof(key_b)), key_b); for (yalv = 0; yalv < KEY_MAX; yalv++) < if (test_bit(yalv, key_b)) < switch ( yalv) < case 0x1c : dial->setMessage("Enter"); dial->show(); break; case 0x66 : dial->setMessage("Home"); dial->show(); break; case 0x3b : dial->setMessage("F1"); dial->show(); break; case 0x3c : dial->setMessage("F2"); dial->show(); break; default: dial->setMessage("Unknow for now"); dial->show(); > > >
How does #define test_bit(bit, array) (array[bit / 8] & (1
Thank you for that answer, I’m quite new to c++ and linux and from what I’ve understood this code is supposed to check that a specific bit is set to 1 in a specific byte. (Maybe I’m totaly wrong I did not get many help on the explanation of this from now).But when I press a key in debug I can see one bit in my byte change from 0 to 1 but I never enter in my test_bit condition. Maybe it’s is simply a problem of misunderstanding.
array[bit / 8] divides bit number by 8 to address the proper byte of array . bit % 8 computes remainder of division by 8 (actually, separates last 3 bits storing 0 . 7). 1
array should be a variable (or expression) with type array of unsigned char . ( char or signed char might work as well but signed-ness can cause issues.)
From your previous comment I change with this hoping it’s better : #ifndef test_bit #define test_bit(bit, array) (array[bit / 8] & (1
[Solved]-What’s the meaning of BIT() in this linux kernel macro?-C
looks like you can find the answer inside the first header file included, i.e. bitops.h!
i.e. BIT defines a bit mask for the specified bit number from 0 (least significant, or rightmost bit) to whatever fits into an unsigned long.
So BIT(10) should evaluate to the numeric value of 1024 (which is 1
The BIT macro shifts the value 1 left by the value given to it, so BIT(10) == (1
BIT is a macro defined in include/linux/bitops.h in the kernel tree:
So BIT(10) is basically an unsigned long with tenth bit set.
Related Query
- Difference of ALIGN() and round_up() macro in the Linux kernel
- Fixed 64 bit integer in Linux Kernel
- likely()/unlikely() macros in the Linux kernel with a segmentation fault
- What ‘s the meaning of the number 1 in SIG_IGN macro definition?
- Does the qstr struct in a kernel dentry hold the filename of a Linux file?
- Linux Kernel 4.2.x: Why does the expected system call address not match the actual address when checked?
- stdio.h — whats the name of libfile and where can I find it Linux
- What is the best practice to intercept an exception handler in Linux kernel
- why don’t the memcpy and sprintf work in this kernel code?
- Can’t unload the Linux kernel module after BUG() call
- Functions with the same signature in linux kernel
- C- whats the problem with this code? I try to multiply one matrix in another matrix in func and get new matrix but i get segmentation fault
- Understanding the PID hash table in the linux kernel
- Linux kernel : Current macro working
- In the Linux kernel (module programming) how to get threadids from a multithreaded process?
- Can’t figure out the meaning of this recursive code
- I want to make a GUI for a game coded in C for Linux Platform.Are there any API`s to do this or what woulb be the easiest way to make a gui
- Understanding hlist_bl_for_each_entry_rcu macro in Linux Kernel
- Does this macro violate the STRICT ALIASING RULE?
- Counting the number of siblings a process has, Linux kernel
- How does the Linux kernel create the /proc/$pid/maps file?
- Linux kernel container_of macro example compilation errors
- Adding paths to header files in the Linux kernel
- how to limit the time of a task’s syscall in the linux kernel
- Linux kernel module wireless interface bit rate
- Linux Kernel Device Driver Write Callback is writing data across the entire allocated memory space for the device driver
- when i edit the linux kernel code, what is the process of building and running the modified kernel?
- How does the list_add function for linux kernel linked lists work?
- Working of Raw Sockets in the Linux kernel
- My Linux kernel module is not receiving the correct User-space Application PID
More Query from same tag
- Am I passing structures by reference wrong?
- Compilation error, undefined reference to Test(), multi file C program
- Why am I getting this error? ‘STDOUT_FILENO’ undeclared (first use in this function)
- Different results when iterating through string vs printing a specific position (C)
- Help with simple linux shell implementation
- Passing a C function pointer to emscripten callback
- sort String by Character in C Programming
- Confusion about the pthread_attr_t definition
- What is a C extension?
- Gcc compiler optimization function inlining
- Is my function correctly returning a pointer to a struct?
- C — char pointers with getline() to read from a text file. Why does this happen? How to improve my workaround?
- Why does giving ‘&’ for a string in scanf() give no error?
- pthread_join hangs accordingly to random global variable value
- How to listen to all keypresses in Winapi and cancel some of them?
- How do I make a while loop that will keep taking string input until i input nothing?
- How to properly display the value from a usb device [ansi c]
- Importing file contents into a struct in C
- How to cancel waitpid if child has no status change?
- How endianness effects on bitfields when a bitfield is greater than size of byte
- Rules of main() function when running at non-startup are the same as at startup?
- Hashing, linked list, delete node
- Relaying optional arguments in a wrapper for a variadic function
- Debugging C program with two arguments passed
- How do you count the frequency of which a word of n length occurs within a string
- Use of extern: getting ‘file not found’
- Preprocessor macros: any way to get a unique variable name and reuse it?
- How to check if an int is within +/- some percentage
- How do you correctly pass a pointer between calls with Python C-API
- qsort, compare to middle value, why cannot be equal?
Bit macro in linux
Special permission bits setuid: s owner permission bit 4 Concept description: Allows all ordinary users to obtain the ability of the owner of the specified file. Function description: Special command .
Some important macros in the kernel
There are a lot of macros used in the process of reading the source code, so as a beginning, there are many macro functions that are extremely powerful but also very difficult to understand. This arti.
Linux system kernel and kernel modules
The current kernel has modular features The location of the kernel and kernel modules: Kernel: /boot/vmlinuz or /boot/vmlinux-version Required for kernel decompression: RAM Disk: /boot/inittramfs kern.
Operating system: why to distinguish between user mode and kernel mode
Article Directory foreword 1. The distinction between user mode and kernel mode 2. Why should we distinguish between user mode and kernel CPU instruction set permissions 3. Switching between user mode.
How to distinguish the root under the Linux system?
/boot This directory stores the Linux boot files and kernel by default. /initrd Its English meaning is boot loader initialized RAM disk, which is the memory disk initialized by the boot loader. In lin.