Skip to main content

Questions tagged [bitwise-operators]

low-level, primitive operations that directly manipulate bit patterns and binary numerals

Filter by
Sorted by
Tagged with
6 votes
3 answers
635 views

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 ...
The Fool's user avatar
  • 192
2 votes
2 answers
277 views

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 ...
Tim's user avatar
  • 5,555
1 vote
2 answers
2k views

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)...
Anon's user avatar
  • 3,649
4 votes
1 answer
5k views

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 ...
Prime's user avatar
  • 143
-5 votes
5 answers
300 views

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: ...
the_endian's user avatar
  • 1,152
2 votes
3 answers
732 views

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 ...
the_endian's user avatar
  • 1,152
1 vote
3 answers
976 views

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 ...
Louis Thibault's user avatar
83 votes
7 answers
17k views

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 ...
yoyo_fun's user avatar
  • 2,297
2 votes
5 answers
498 views

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, ...
DumbButterly's user avatar
10 votes
2 answers
3k views

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(...
Qqwy's user avatar
  • 4,947
4 votes
5 answers
5k views

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 ...
99Con's user avatar
  • 185
1 vote
2 answers
869 views

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 <<&...
Martijn's user avatar
  • 121
3 votes
3 answers
340 views

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 ...
FZE's user avatar
  • 469
3 votes
1 answer
866 views

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 ...
Snoop's user avatar
  • 2,758
27 votes
2 answers
26k views

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 ...
quant's user avatar
  • 1,386
0 votes
2 answers
4k views

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. ...
GermanShepherd's user avatar
11 votes
3 answers
52k views

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) { ...
user avatar
2 votes
2 answers
224 views

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 /* ...
nobby's user avatar
  • 123
0 votes
1 answer
162 views

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 ...
user2630165's user avatar
6 votes
4 answers
6k views

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 ...
Stumbler's user avatar
  • 179
2 votes
1 answer
1k views

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 ...
mjf's user avatar
  • 151
0 votes
3 answers
761 views

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 ...
Rodrigo's user avatar
  • 1,077
75 votes
2 answers
9k views

Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^...
SF.'s user avatar
  • 5,236
3 votes
2 answers
2k views

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 ...
fordeka's user avatar
  • 439
8 votes
4 answers
29k views

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 ...
Kuldeep Jain's user avatar