low-level, primitive operations that directly manipulate bit patterns and binary numerals
0
votes
1answer
285 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 ...
0
votes
2answers
422 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 ? ...
27
votes
4answers
17k 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. ...
3
votes
1answer
432 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 + - ...
2
votes
2answers
123 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
2answers
277 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.
...
26
votes
10answers
4k 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 ...
1
vote
3answers
4k 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)
{
...
5
votes
1answer
834 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. ...
0
votes
3answers
5k 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 ...
0
votes
1answer
137 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 ...
2
votes
2answers
1k 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.
...
1
vote
4answers
762 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 ...
4
votes
2answers
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 ...
1
vote
1answer
306 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 ...
2
votes
1answer
418 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 ...
4
votes
3answers
13k 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 ...
11
votes
6answers
6k 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 ...
0
votes
3answers
457 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 ...
2
votes
2answers
698 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 ...
42
votes
2answers
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 (&, |, ...
0
votes
2answers
465 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 ...
18
votes
15answers
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 ...
-2
votes
2answers
6k 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 ...
5
votes
3answers
11k 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 ...
6
votes
3answers
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 ...
11
votes
5answers
3k 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 ...
29
votes
13answers
2k 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.
...
4
votes
3answers
277 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, ...