# Tagged Questions

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

103 views

### Is there a conceptual 2-bit-input logical operation that does: 0 0 = 0, 0 1 = 1, 1 0 = 0, 1 1 = 0?

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 ...
967 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 ...
115 views

### Getting an array index (0,1,2,..8) from bit masking value (1,2,4,8..256) without using log2(n). Maybe a design issue

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 ...
130 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 <<&...
48 views

### How to calculate big O notation according to number width?

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 ...
138 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 ...
63 views

### Bits - Least-Significant/Lowest is 0th or 1st; zero or one indexed

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, ...
125 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 ...
390 views

### Is there a way to do bitwise operation with more than two state

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 ...
591 views

### How do you set and check a single bit in a programming language that does not support bitwise operations?

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 + - *...
417 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. ...
5k views

### When I test out the difference in time between shifting and multiplying in C, there is no difference. Why?

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 &...
7k views

### What does “ (int) value & 0x1, (int) value & 0x2, (int) value & 0x4, (int) value & 0x8 means”

The "value" ranges from 0 to 15 (it's possible values). When will those 4 "if"condition be met? If my (int)value = 2 does this mean 0010? if ((int)value & 0x1) { ...
1k views

### In Java, why use bit hacks when non-bitwise technique it is more readable?

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. ...
133 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 /* ...
13k views

### How are negative signed values stored? [duplicate]

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 ...
144 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 ...
3k views

### Speeds of << >> multiplication and division

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. ...
1k 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 ...
2k views

### Why is ~0 a Sequence of 1s [closed]

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 ...
368 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 ...
510 views

### Clearing the lowest set bit of a number

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 ...
501 views

### Bits manipulation in C [duplicate]

I am now studying computer system in my college, and now we are learning about bits manipulation in C. And I am just curios, how can by learning bits manipulation can help us to make a reliable code ? ...
553 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 ...
814 views

### Concept of bit fields

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 ...
3k 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 (&, |, ^...
26k views

### Using scoped enums for bit flags in C++

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. ...
609 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 ...
18k views

### Is there something special about the number 65535?

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 ...
20k 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 ...
8k views

### Flipping the desired bit of an integer number [duplicate]

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 ...
2k views

### How does bit flipping / complementing work?

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping. So, 5 is 0101. When someone says to me "flip those", it ...
3k views

### How good does a well-rounded programmer need to be with bit-wise operations? [closed]

I have been browsing some OpenJDK code recently and have found some intriguing pieces of code there that has to do with bit-wise operations. I even asked a question about it on StackOverflow. ...
4k views

### What is the benefit of studying bitwise operators? [duplicate]

What is the benefit of studying bitwise operators (Bitwise Not, Bitwise AND, Bitwise OR, Bitwise XOR, Left Shift, Signed Right Shift, Unsigned Right Shift etc.)? Will we really use these operators in ...
296 views

### How about multiple bits performance in common databases?

If there are 20 bit columns (non-null boolean), is it better to explicitly hold them in an integer type (int32)? Or, will the underlying database merge them in a single integer? I'm using PostgreSQL, ...
9k views

### What's your favorite bit-wise technique? [closed]

A few days ago, StackExchange member Anto inquired about valid uses for bit-wise operators. I stated that shifting was faster than multiplying and dividing integers by powers of two. StackExchange ...
3k views

### What are bit operators good for? [closed]

Programming languages often come with various bit operators (e.g. bitwise left- and right shift, bitwise AND, OR, XOR...). These don't get used though very much, or at least such has my experience ...
8k views

### What are the advantages of using bitwise operations? [closed]

Following reading the latest CodeProject newsletter, I came across this article on bitwise operations. It makes for interesting reading, and I can certainly see the benefit of checking if an integer ...