In C, if you want to hide bit manipulation, you can write a macro:
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
and use it this way to check the nth bit from the right end:
CHECK_BIT(temp, n - 1)
In C++, you can use std::bitset.
More Related Contents:
- Determine if int is not 0 with no if statements
- Invalid solution for code challenge with operator restrictions
- How do you set, clear, and toggle a single bit?
- Best practices for circular shift (rotate) operations in C++
- Position of least significant bit that is set
- How to create a byte out of 8 bool values (and vice versa)?
- Examples of when a bitwise swap() is a bad idea?
- C/C++: Force Bit Field Order and Alignment
- What is the efficient way to count set bits at a position or lower?
- What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?
- How do I set, clear, and toggle a single bit?
- In C/C++ what’s the simplest way to reverse the order of bits in a byte?
- Weird behavior of right shift operator (1 >> 32)
- Should I use #define, enum or const?
- How to add two numbers without using ++ or + or another arithmetic operator
- get absolute value without using abs function nor if statement
- What happens when you bit shift beyond the end of a variable?
- What’s the fastest way to pack 32 0/1 values into the bits of a single 32-bit variable?
- Efficient bitwise operations for counting bits or find the right|left most ones
- Performance wise, how fast are Bitwise Operators vs. Normal Modulus?
- Branchless code that maps zero, negative, and positive to 0, 1, 2
- Fastest way to produce a mask with n ones starting at position i
- How to set, clear, and toggle a single bit?
- Fast divisibility tests (by 2,3,4,5,.., 16)?
- How can I test whether a number is a power of 2?
- Dijkstra Program
- Performance of built-in types : char vs short vs int vs. float vs. double
- GCC -Wuninitialized / -Wmaybe-uninitialized issues
- Where are string literals stored, and can I modify them?
- C++ trying to swap values in a vector