3
\$\begingroup\$

This is the code I came up with:

void set_bit_at_pos(int* num, int pos, bool val)
{
    if (val) {
        int mask = 1 << pos;
        *num |= mask;
    }
    else {
        int mask = ~(1 << pos);
        *num &= mask;
    }
}

I'm new to bitwise operations, so this approach may not be the best. Nevertheless any suggestions are appreciated.

For the following main:

int main()
{
    int num = 5;
    cout << num << endl;
    set_bit_at_pos(&num, 1, true);
    cout << num << endl;
}

(I know about using namespace std;)

This is the output:

5
7
\$\endgroup\$

closed as off-topic by pacmaninbw, dfhwze, πάντα ῥεῖ, Mast, Grajdeanu Alex. Jul 23 at 9:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – pacmaninbw, dfhwze, πάντα ῥεῖ, Mast, Grajdeanu Alex.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ It would be very helpful if you demonstrated how this function was expected to be used. In most cases I would recommend using unsigned int rather than int for num. There aren't very many reasons to adjust bits in signed integers and removing a bit may make it go negative. \$\endgroup\$ – pacmaninbw Jul 19 at 21:34
  • \$\begingroup\$ Well, that's the point, @pacmaninbw , I'm not sure if it works correctly. I don't know if there's an off by 1 error, maybe bits start from 0 as well, and not from 1 which is I believe the way I wrote it. I'll provide some output though. \$\endgroup\$ – Mark Ceitlin Jul 19 at 22:36
  • 2
    \$\begingroup\$ @MarkCeitlin Bitwise operations on signed integers are unsafe. If you want to manipulate bits on signed integers, it's better to cast to unsigned integers, manipulate, and cast back to signed integers \$\endgroup\$ – Cacahuete Frito Jul 20 at 0:11
  • \$\begingroup\$ You should'nt ask a question if you're not sure your code works. At least you could have written some tests to verify the correctness of your method. Here's a great post about bit operations: stackoverflow.com/questions/47981/… \$\endgroup\$ – dfhwze Jul 20 at 5:40
  • \$\begingroup\$ I have retagged the question. This is clearly c++ code not c. \$\endgroup\$ – πάντα ῥεῖ Jul 20 at 6:36
1
\$\begingroup\$

This is simpler:

#include <stdint.h> // #include <cstdint> in C++

void write_bit_32b(uint32_t *num, uint8_t pos, bool val)
{

        *num &= ~(UINT32_C(1) << pos);
        *num |= (uint32_t)val << pos;
}

Use fixed-width integers if you can (see above).


Use int main(void):

C17::6.11.6:

Function declarators The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.


This may be interesting to you:

https://stackoverflow.com/q/47981/6872717

\$\endgroup\$
  • \$\begingroup\$ The OP obviously asks about c++ code. Thus citing c standards is pretty useless here. \$\endgroup\$ – πάντα ῥεῖ Jul 20 at 6:39
  • \$\begingroup\$ There is a subtle difference between int main() and int main(void) in C. In C++ though the use of (void) is equivalent to () it is considered archaic and to avoid confusion with C considered bad practice by most coding standards. There are only two valid declarations for main int main() and int main(int, char**) See: n4820 6.8.3.1 main function [basic.start.main] \$\endgroup\$ – Martin York Jul 22 at 6:09
  • \$\begingroup\$ @MartinYork In C, in a function definition, () and (void) are the same (unlike in function declarators other that definitions; don't remember and can't find the paragraph). Given that in C++ they are absolutely equivalent, although the wording of the standard is unfortunate, I would interpret it as in C17::5.1.2.2.1: "or equivalent" \$\endgroup\$ – Cacahuete Frito Jul 22 at 11:52
  • \$\begingroup\$ BTW, the only reason for the (void) comment is that this question was originally asked as a C question, not C++ \$\endgroup\$ – Cacahuete Frito Jul 22 at 11:53
  • \$\begingroup\$ In C () and (void) are subtly different in that void indicates an unknown number of parameters. I was unaware that declaration and definition have different rules for void in C. But that just reinforces my conviction that you should use the version without void for both consistency and readability. Also since the rules are obscure using the one with the least exceptions would be best from a maintenance stand point. \$\endgroup\$ – Martin York Jul 22 at 16:10

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