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
636 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
6 answers
667 views

In software engineering, often developers will utilize three different states of a signed integer, as a trilean: This tends to be quite typical: -1 - Represents something akin to a null pointer, as in ...
Anon's user avatar
  • 3,649
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,565
3 votes
5 answers
4k views

I’m curious as to why the bitwise AND of any even number with 1 is equal to 0? I’ve looked at the binary representations of an odd number and 1, and have found that the following is always true for ...
p32094's user avatar
  • 187
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
0 votes
1 answer
287 views

Moved I originally posted this on SoftwareEngineering because that's where this related question was; but having looked into the Help in detail, I think my question is more on-topic for stackoverflow,...
LarsH's user avatar
  • 153
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
4 votes
3 answers
809 views

I'm working with an old C module that was originally ran on Power PC architecture and compiled with gnu 3.0.6 I'm porting it to run in a VS2012 project on Intel hardware. The module creates a 3D ...
Sam's user avatar
  • 165
-5 votes
5 answers
301 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
1 vote
2 answers
603 views

I have implemented a simple bit vector class. However, I have some problems with understanding, how to push data to it. In a standard vector, push_back inserts new element et the end. A similar ...
Martin Perry's user avatar
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
1 answer
389 views

In the C++, there are 6 bitwise operators: Symbol Operator & bitwise AND | bitwise inclusive OR ^ bitwise XOR (eXclusive OR) << left shift >> right shift ~...
Anon's user avatar
  • 3,649
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
0 votes
1 answer
198 views

I receive sensor data as a binary stream of bytes. This stream is not always the same length, and does not include the same data set each time. If the sensor did not send a field, it is simply absent, ...
foxtrotuniform6969'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
0 votes
1 answer
283 views

With one boolean, it is simply 0 and 1, and it's straight-forward to query. With two booleans, it becomes necessary to specify what each number means, ex: 0 F-F 1 F-T 2 T-F 3 T-T Here it's easy to ...
davidtgq's user avatar
  • 431
2 votes
5 answers
500 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
40 votes
1 answer
13k views

Not that it's really a problem for anyone that has faced this syntactic issue before, but I see a wild amount of confusion stemming from the use of the caret (^) as the XOR operation in lieu of the ...
Qix - MONICA WAS MISTREATED'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
1 vote
2 answers
223 views

Reason: In an LR35902 (the processor in the Game Boy) assembly program of mine, I'm trying to figure out what bits have changed to 1's when comparing A with B, which would need a bitwise operation ...
Tachytaenius's user avatar
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
2 votes
1 answer
3k views

I'm working on a component where I put in data and I get different data as a result. The input is always the same (3 Objects). From these 3 Objects up to 9 other Objects can be calculated. One ...
tzippy's user avatar
  • 167
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
0 votes
1 answer
515 views

I'm trying to understand big O with the bitwise operations. I have 2 functions those are solving the same question from different perspective. num1BitsSecondSolution starts to shift the number right ...
FZE's user avatar
  • 469
3 votes
3 answers
341 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
1 vote
3 answers
3k views

Question Is there a rough consensus if the bitmask 0x01 is properly said to have the "zeroth" bit set, or the "first" bit set? If there isn't rough consensus that there's a generally right answer, ...
mtraceur's user avatar
  • 269
3 votes
1 answer
875 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
0 votes
1 answer
1k views

I know how make bit operation. I'm wondering if you need more than 2 states is there a way to do it using bit instead of array and % operators. Because those are really slow. So in case the array ...
Juan Carlos Oropeza's user avatar
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
3 votes
1 answer
973 views

I'm using a programming language (more a scripting language) that does not support any bitwise operators like AND, OR, XOR, NOT (and shift as well). Common arithmetic and logical operations like + - *...
tigrou's user avatar
  • 279
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
29 votes
11 answers
12k views

I have been taught that shifting in binary is much more efficient than multiplying by 2^k. So I wanted to experiment, and I used the following code to test this out: #include <time.h> #include &...
NicholasFolk'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
9 votes
3 answers
3k views

Is there any legitimate use for bit manipulation hacks in higher-level languages such as Java? I can see them being useful in speed-sensitive low-level and computation-intensive programs, e.g. ...
blgt's user avatar
  • 306
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
19 votes
4 answers
96k views

I was watching this video on the maximum and minimum values of signed integers. Take an example of a positive signed value - 0000 0001 The first bit denotes that the number is positive and the last ...
discussedtree's user avatar
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
14 votes
3 answers
43k views

You can use << to multiply and >> to divide numbers in python when I time them I find using the binary shift way of doing it is 10x faster than dividing or multiplying the regular way. ...
Crizly's user avatar
  • 327
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
5 votes
2 answers
2k views

Why is ~0 a sequence of 1 bits and not just a single bit? Where do the extra 1 bits come from? Does this mean ~1 a sequence of 0 bits or just a single 0 bit? My understanding was that the ~ operator ...
clicky's user avatar
  • 193
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
5 votes
1 answer
7k views

I can see in this tutorial on bit manipulation, under the heading "Extracting every last bit", that - Suppose we wish to find the lowest set bit of x (which is known to be non-zero). If we ...
theharshest's user avatar
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
4 votes
2 answers
2k views

Whenever I read a code like this: struct node { int x : 2; int p : 4; }n; with bit fields involved, I get really confused, as to how they are represented in memory, what is ...
user1369975's user avatar
  • 1,319
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
77 votes
8 answers
110k views

An enum X : int (C#) or enum class X : int (C++11) is a type that has a hidden inner field of int that can hold any value. In addition, a number of predefined constants of X are defined on the enum. ...
Daniel A.A. Pelsmaeker's user avatar
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
17 votes
3 answers
66k views

2¹⁶-1 & 2⁵ = 2⁵ (or? obviously ?) A developer asked me today what is bitwise 65535 & 32 i.e. 2¹⁶-1 & 2⁵ = ? I thought at first spontaneously 32 but it seemed to easy whereupon I thought ...
Niklas Rosencrantz's user avatar
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
0 votes
2 answers
19k views

Suppose you were given an integer number in decimal notation. This when represented in binary will be a series of 0's and 1's. This sequence varies in length with the magnitude of the number. Now ...
Arpith's user avatar
  • 307