2

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 integers.

Questions:

  • Are bitwise logical operators also used for implementing operations on values of higher types (e.g. integers)?

  • Are bitwise shifting operators also used for bits operations?

Thanks.

1
  • To clarify, what I said in my (now deleted) comment is that it's not at all clear from the OP's question what they consider a "higher type." Higher than what? A word? I had to go back and look up the computing definition of "word." It is typically used to mean "the natural unit of data used by a particular processor design," i.e. 32 bits on a 32 bit processor. OK, sure. While I don't think we need to delve into bitset types to make the distinction, yeah, essentially there is no difference between a 32 bit "word" and a 32 bit int in C. Commented Oct 18, 2020 at 13:44

2 Answers 2

3

I think the main misconception expressed in this question here is that there is a difference between a "word of bits" (or bitset) and "integers" in C.

In short: there is not.

Specifically in languages which support operator overloading like C++, it would be possible to implement something like a Bitset type. This could represent an array of bits, and not a number. However, this is not the way how it is done in C (or C++) with the basic datatypes int or unsigned int and the mentioned operators.

An operator like & just takes two integers, interprets them in a canonical way as bits, applies the AND operation bitwise and returns the corresponding bit array by reinterpreting it again as an integer. Same holds for the other operations you mentioned.

A shift operation like << does work in a similar fashion. It's result is actually identical with the arithmetic multiplication by a factor of 2. So if one interprets << as a shift of bits or as an arithmetic operation on an integer is only a matter which point of view one prefers. Technically, there is no difference in the language expressed by types.

Hence, "bit operations" belong to the category of "operations for integer or some C types", which hopefully explains why the distinction of the usage of "bitwise operators" in the question title does not make much sense.

3
  • This is a really interesting perspective. I think the crucial counterpoint would be that, although C itself does not formalise a distinction between an int and a "bitset" in the type system, the C programmer himself almost invariably does make the distinction.
    – Steve
    Commented Oct 18, 2020 at 12:58
  • @Steve: that is true, but I don't see it as a "counterpoint". The idea of using a fixed length array of bits to represent numbers is probably one of the most fundamental basics built into computer hardware for decades. Therefore, the fact languages like C (and many other) have operators which support actually this mental switch between those kind of abstractions does not seem to be very astonishing.
    – Doc Brown
    Commented Oct 18, 2020 at 13:25
  • I agree entirely. I updated my answer earlier. The fact that ints and bit arrays are the same thing both in explicit C code and in hardware, doesn't mean they aren't different conceptually in the programmer's mind. I think that's key to understanding what the OP meant by his question.
    – Steve
    Commented Oct 18, 2020 at 22:44
1

I've actually upvoted this question, as a perfectly legitimate question from someone attempting to learn.

The first thing to say is that it's conventional to distinguish between either "bitwise operators", or "logical operators". A "bitwise logical operator" sounds like a conflation according to this conventional distinction, even though it's possible to make sense of the term as you are using it (since a bitwise operator typically applies a logical operator to an array of bits).

The "bitwise logical operators" and the "bitwise shift operators" are rarely if ever distinguished - both simply falling under the heading of "bitwise operators".

A "logical operator", conventionally, means an operator that folds multi-bit operands down to a single bit, and produces a result which is a single bit. Hence, you have the distinction between "bitwise NOT" (a straightforward inversion of all bits) and "logical NOT" (folding all bits with an OR operation to yield a single bit, followed by an inversion of that single bit - or something similar depending on the exact implementation and architectural details).

So your question concerns what are normally referred to simply as "bitwise operators".

To answer the first of your questions, bitwise shifting is certainly used on integers. For example, multiplying an integer by 2, is generally equivalent to a bitwise left-shift of the same integer.

Your second question is a little ambiguous, but if it means "are bitwise shift operators used to select or manipulate individual bits", then yes, they can be.

EDIT: I don't know whether this question changed since my first answer, but I think I've now seen it more clearly, particularly in conjunction with Doc Brown's thought-provoking answer.

The OP observes that the bit-shift operators are used for arithmetical purposes (multiplication and division by 2).

He thus first asks, are the other bitwise "logical" operators (like AND, OR, NOT) used similarly for arithmetical purposes? The answer is yes they can be - AND could be used for arithmetical rounding (for example, rounding down to an even number), NOT could be used for arithmetical negation (in a one's complement representation).

And having identified how the bit-shift operators are used for arithmetical purposes, I think the question he asks secondly is whether they have any legitimate non-arithmetic use when applied to a bit array?

Again, the answer is yes, it can be used as part of a bit buffer for example. The bit-shift operators are also used for assembling (and dissembling) arrays of bits or assembling/dissembling packed integers.

1
  • I know this site is getting old but I’d like to believe there’s still room for this. Commented Oct 17, 2020 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.