Questions tagged [bitwise-operators]
low-level, primitive operations that directly manipulate bit patterns and binary numerals
57 questions
6
votes
3
answers
635
views
What is the term for a integer variable containing bit fields?
If I have a variable that is intended to to be used with bitmasks, to
retrieve values, i.e. bit fields and flags, what is the academic term
for such a variable?
If the (bit) fields are a analogous to ...
2
votes
2
answers
277
views
Are bitwise operators in C used both for bit operations and for operations for integer or some C types?
In C,
Bitwise logical operators &, |, ^ is used for selecting bits in a word.
Bitwise shifting operators >> and << can be used for implementing multiplication and division between ...
1
vote
2
answers
2k
views
In C++, Why do bitwise operators convert 8 or 16 bit integers to 32 bit?
Is there a logical reason why the integer is upgraded to 32+ bits?
I was trying to make an 8bit mask, and found myself a bit disappointed that the upgrade will corrupt my equations.
sizeof( quint8(0)...
4
votes
1
answer
5k
views
Semantics of simulating a 64bit integer with two 32bit integers (hi, lo)
I am working in a system which only supports 32bit integers, in order to have a 64bit (unsigned) integer I decided to simply use two 32bit integers with one being the upper 32 bits (hi), and the other ...
-5
votes
5
answers
300
views
Do decimal equivalents to binary number values hold significance in software programming?
It seems as though in software engineering, we care more about these "on and off switch" usages of binary numbers more than the actual values of them numbers... For example, say I have the number:
...
2
votes
3
answers
732
views
What effects does memory space have on bitwise shifts?
It is true that the bitwise left shift operation (shl) doubles the value of the integer being shifted. However, when constrained to a finite space, such as 8 bits for example, left shift will begin to ...
1
vote
3
answers
976
views
How can I query, increment & decrement arbitrary-length integers encoded into a bit-array?
I'm in the process of implementing a counting Bloom filter. This data structure is defined as a bit-array and a "width" parameter, W.
The bit array stores unsigned integers, whose size is determined ...
83
votes
7
answers
17k
views
Why are bit masks called "masks" and what purpose do they serve?
Why are "bit masks" called like this?
I know that they are mainly used for bitwise operations and the usage of bit masks is more efficient than the usage of separate variables.
However my question ...
2
votes
5
answers
498
views
Shortening a boolean AND with third operand
I'm trying to calculate the sum of 2 bits using basic binary arithmetic and currently, I'm doing this:
function Add(bool a, bool b, bool carry)
{
return
{
Result: a ^ b ^ carry,
...
10
votes
2
answers
3k
views
Is it possible to define all bitwise operators using a 'bitwise nand' similar to how all boolean logic can be built using just 'boolean nand'?
Nand is known as a 'universal' logic gate, because it allows you define all other boolean logic gates:
not(x) = nand(x,x)
and(x, y) = not(nand(x, y))
or(x, y) = nand(not(x), not(y))
nor(x, y) = not(...
4
votes
5
answers
5k
views
How does bitwise information storage in (32-bit) int variables work?
In this book I'm reading I'm going over bitwise operators. Its says the following in the book.
Bitwise operations can potentially store a lot of information in a small amount of memory. Many traits ...
1
vote
2
answers
869
views
LSB-propagating left shift, conceptual equivalent of sign-propagating right shift [closed]
How would I write something that fills with the right-most bit (<<< is used to denote this non-existent operator):
1 <<< 7: "11111111" and 0 <<< 7: "00000000"
9 <<&...
3
votes
3
answers
340
views
Is there any low level way to get shifted or unshifted bits which results from bitwise operations?
I was playing with bitwise operations and a question about counting true bits of any positive integer value, so I solved the problem with bit shifting, so I just thought if there would be some way to ...
3
votes
1
answer
866
views
Overflow Exception Checking Problem
Background
I have to call a method that needs to return a signed integer (see code block below) after converting from an unsigned integer. The reason for this is that I have to do bit-wise math that ...
27
votes
2
answers
26k
views
Is there any advantage to c-style bit manipulation over std::bitset?
I work almost exclusively in C++11/14, and usually cringe when I see code like this:
std::int64_t mArray;
mArray |= someMask << 1;
This is just an example; I'm talking about bit-wise ...
0
votes
2
answers
4k
views
boolean operations in C using bitfields
I am trying to implement boolean data type in C. Basically, I am working with sets.
The following code can be used to access each bit but I am unsure whether I can represent sets using this method.
...
11
votes
3
answers
52k
views
What does "(int) value & 0x1, (int) value & 0x2, (int) value & 0x4, (int) value & 0x8" mean?"
The "value" ranges from 0 to 15 (its possible values). When will those 4 "if" conditions be met? If my (int)value = 2 does this mean 0010?
if ((int)value & 0x1)
{
...
2
votes
2
answers
224
views
gcc -S seems a bit misshapen with shifting and ANDing bits
Example:
int c = 4;
int p = 5;
if (p & (1 << c))
printf("ok\n");
else
printf("nop\n");
gcc -S:
movl -4(%rbp), %eax /* eax holds the variable c */
movl -8(%rbp), %edx /* ...
0
votes
1
answer
162
views
Why Num&sizeMinusOne faster than num&(size-1)
I've been told that when I have a hash table of size m and m=2^k, I can use the & operator as num & (size-1) instead of num % size, to fit the hashCode to my table size.
I've also been told ...
6
votes
4
answers
6k
views
Why do higher level languages have neither xor nor nand short-circuit operators?
While many higher level languages have bitwise (exclusive or) and bitwise (exclusive and), for instance C, C++, Java, etc. I'm curious why the ( vastly more useful ) logical short-circuit operators ...
2
votes
1
answer
1k
views
Next power of 2 for a number (in search for better "bit-twiddling" way)
I just wonder if there exists better (i.e. faster?) way to get the next
power of 2 for a given number than the following one (maybe some
better sort of "bit-twiddling" hack is possible?) ...
static ...
0
votes
3
answers
761
views
Using Power of 2 numbers to represent types
Let's say that we have some values, represented by power of 2:
TYPE_1 = 1
TYPE_2 = 2
TYPE_3 = 4
TYPE_4 = 8
...
I need to store some of these types in one value.
Example:
To represent TYPE_1 with ...
75
votes
2
answers
9k
views
Why do bitwise operators have lower priority than comparisons?
Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^...
3
votes
2
answers
2k
views
What kind of specific projects can I do to master bitwise operations in C++? Also is there a canonical book? [closed]
I don't use C++ or bitwise operations at my current job but I'm thinking of applying to companies where it is a requirement to be fluent with them (on their tests anyway).
So my question is: Can ...
8
votes
4
answers
29k
views
Ternary operator (condition ? foo : bar) and the XOR (^) operator
I have read in a recent code review that both ternary operator (condition ? foo : bar) and the XOR operator ^ are rarely used in Java. Is it true?
If yes, is this because they are less readable? or ...