Skip to main content

All Questions

Tagged with
2 votes
2 answers
250 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 ...
Tim's user avatar
  • 5,525
2 votes
1 answer
3k 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 ...
tzippy's user avatar
  • 167
2 votes
3 answers
303 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 ...
FZE's user avatar
  • 459
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. ...
GermanShepherd's user avatar
29 votes
11 answers
11k 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 &...
NicholasFolk's user avatar
2 votes
2 answers
217 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 /* ...
nobby's user avatar
  • 123
19 votes
4 answers
96k views

How are negative signed values stored?

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
2 votes
1 answer
992 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 ...
mjf's user avatar
  • 151
5 votes
1 answer
7k 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 ...
theharshest's user avatar
4 votes
2 answers
2k 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 is ...
user1369975's user avatar
  • 1,299
14 votes
4 answers
12k 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 ...
bit-twiddler's user avatar
  • 2,658