Thread: Changing particular bits and detecting toggled bits

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    5

    Changing particular bits and detecting toggled bits

    Hi, this is my first post on this forum since I've started to learn programming. It would be nice to get help here. I have 2 questions about bit manipulation:
    1. I want to "NOT"(~) particular bits for example in 11001000 I would like to "NOT"(~) 3rd and 4th(counting from right) bit for example. I know I can use bit mask, yes? Even so I would also like to know if there is a way to do that without knowing actual bit word.
    2. I want to count toggled bits in bit word, for example that from this previous bit word I would get "3" meaning 3 togged bits.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nordomus
    1. I want to "NOT"(~) particular bits for example in 11001000 I would like to "NOT"(~) 3rd and 4th(counting from right) bit for example. I know I can use bit mask, yes? Even so I would also like to know if there is a way to do that without knowing actual bit word.
    What do you mean by "actual bit word"? From what I see, you just need to create the mask (e.g., by left shifting 1), then use xor (i.e., the ^ operator) to toggle the desired bits.

    Quote Originally Posted by Nordomus
    2. I want to count toggled bits in bit word, for example that from this previous bit word I would get "3" meaning 3 togged bits.
    I think the usual recommendation is to peruse this webpage on Bit Twiddling Hacks
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Have a look at the std::bitset class. It makes bit manipulation easier.
    C++ Bitset With Example | Owlcation
    std::bitset - cppreference.com

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think the first question is about if there is a way without knowing where the bit is that he wants to toggle; the answer is no. Even if you used bitset, or a mask and an operation like ~ you would have to know where the bits are to get whatever strategy to work.

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Ok I came back to this problem after holidays and here it is what I made for first problem:
    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        uint8_t a, b, c, d, m;
    
        cout << ("Enter character which bits will be inverted:");
        cin >> a;
        cout << ("Enter starting bit range to change:");
        cin >> c;
        cout << ("Enter last bit range to change:");
        cin >> d;
    
        m = ~0;
        m = m >> (8-(d-c+1));
        m = m << (c-1);
    
        b = a ^ m;
        cout << b;
    
        return 0;
    }
    I don't know why it doesn't work, with fixed numbers it works like a charm but with variables it just returns number provided by user. What I did wrong?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try something like
    cout << (int)c << endl;

    If you typed in say 3, and get 3 back, you're good.
    If you get back say 51, then your inputs are characters, not integers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Yeah they are characters, I knew that, still program doesn't work with variables as it should.
    After negating bits the character should change accordingly, instead it just returns input character unchanged. When I change variables directly into numbers. For example 4 to 5 for c and d then it works flawlessly and when it gets "2" (50 asci) it changes it to *(42 asci). So how to make it to work with variables? Also how could I use actual digits instead of characters?(that's not important but I'm curious)

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    The problem is that you are reading in your start and end bit numbers as characters. So if you enter 1 you actually get the decimal value 49. Try this:
    Code:
    #include <iostream>
    using namespace std;
     
    int main() {
        char ch;
        int start, end;
    
        cout << ("Enter character which bits will be inverted:");
        cin >> ch;
        cout << ("Enter starting bit range to change:");
        cin >> start;
        cout << ("Enter last bit range to change:");
        cin >> end;
     
        uint8_t m = ~0;
        m >>= 8 - (end - start + 1);
        m <<= start - 1;
    
        ch ^= m;
        cout << ch << '\n';
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Thank you it actually works now, but honestly I still don't understand why. You changed variables from uint8 to int and if I only change that in my program it works(returning asci but still it's ok). Although exercise I was given requires me to use int8_t, is it impossible with that type?

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by Nordomus View Post
    Although exercise I was given requires me to use int8_t, is it impossible with that type?
    Where is that exercise from?
    If it's from a website, post the link.
    If it's from a book, give the title of the book and post the exercise verbatim.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To be honest, it seems like some kind of bug to me.
    http://www.cplusplus.com/reference/i...perator%3E%3E/

    The standard stream extractors don't recognise the fixed width integer types at all, so you're kind of left at the mercy of whatever the implementation decides to give you.

    Where the implementation decides that uint8_t has any kind of 'char' flavour to it, then >> will give you '1' rather than 1. Which is rather contradictory given the expectation that all you wanted was a small unsigned integer.

    But if your implementation gives you an int flavoured uint8_t, then >> will always give you a 1 (which is what you wanted and expected to begin with).

    I couldn't find anything in the standard which indicates that any of the xxx8_t types will always/sometimes be the underlying char type with the same signedness.

    Mmm, is it even possible to specialise >> to 'do the right thing' with an xxx8_t without the compiler getting carried away and resolving the typedef down to a bare 'char', where you end up with '1' instead of 1?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Quote Originally Posted by john.c View Post
    Where is that exercise from?
    If it's from a website, post the link.
    If it's from a book, give the title of the book and post the exercise verbatim.
    I got this exercise from my friend teacher, not in any book/website afaik.
    Anyway it is somewhat done so thank you all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  2. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  3. tag bits?
    By Intimd8r in forum C++ Programming
    Replies: 0
    Last Post: 07-27-2003, 02:08 PM
  4. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM
  5. bits like int x:4
    By esler in forum C Programming
    Replies: 5
    Last Post: 05-09-2002, 08:02 AM

Tags for this Thread