Thread: how can i modify the bits of a char?

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    how can i modify the bits of a char?

    I'd like to take a char, convert it to its bit-pattern, and be able to modify the bits, is this possible in C++?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes, except for the first part: I don't know why you think there's a difference between a char and its "bit-pattern". But chars are ints (if smaller), and you can do bit manipulations just the same.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I guess I should rephrase: how do I perform those bitwise operations?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, there are six bitwise operators:
    << shift left
    >> shift right
    | bitwise or
    & bitwise and
    ~ bitwise not
    ^ bitwise xor

    The first two just shift the number of bits (so x << 1 shifts the bits in x one spot to the left, and fills in with 0). ~ reverses bits; the other three do the boolean operator bit-by-bit.

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    okay, cool. so's I'd like to get bits 5 and 6 of a char, and modify them (because these bits control which grouping of 32 chars I'm in). Bear with me, this is all part of a program I'm writing.

    How do we go about that?

    If I say:
    Code:
    char a_char;
    cin >> a_char;
    
    // get the 5th and 6th bits:
    bool bit5 = a_char << 5;
    Perhaps you can tell where I'm getting confused about how to use the bitwise operations to perform this.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Bit shifting is going to move the bits, not tell you what's where. You should look up "bitwise operation" in Wikipedia.

    You might want something like this:
    Code:
    #define BIT_FIVE 0x10
    #define BIT_SIX 0x20
    
    bool bit5 = a_char & BIT_FIVE;
    (As a side note, you really need to write a letter to the publisher of your C++ book and tell them how bad their index is, since it doesn't have bit operations in it.)

    Edit to add: of course 0x10 is the fifth bit from the right; the fifth bit from the left would be 0x08 in a 1-byte char, where "right" means "least significant" and "left" means "most significant".
    Last edited by tabstop; 02-03-2008 at 01:10 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Let me explain what tabstop's code would do.
    It basically uses a mask to get only the bits concerned and save them in another variable.
    assume a_char = (in binary) 01110001
    76543210
    Please note that (according to my books) bits start counting from 0.
    So you want the 5th bit, which would be bit 4 here, right?

    What you do is you create a mask, that will omit all other bits and only keep that one bit, whether it is 0 or 1.
    This mask would be 00010000

    Now, if you compare these two using & this is what would happen:
    a_char: 01110001
    mask: 00010000
    -------------
    result: 00010000

    As you can see, since all the bits are set to zero except bit #4 then only that bit will be saved and the others all set to 0.

    Now, if you want to save bits #4,5 then you'll just have to modify the mask. I'll leave that for you to solve (it's really easy).
    I might not be a pro, but I'm usually right

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The binary and operator only "keeps" the bits which are 1 in the mask. It doesn't matter what state the other bits than the fifth is because they'll be tossed away. And if the 5th bit is 0, well, then you get the result 0 because it extracts that bit nevertheless. If it's one, then the result is one because it was extracted.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by tabstop View Post
    (As a side note, you really need to write a letter to the publisher of your C++ book and tell them how bad their index is, since it doesn't have bit operations in it.)
    It seems many C++ books omit this information, it's as if they think this is just old useless C-Style programming that C++ programmers shouldn't concern themselves with.

    Anyways, back to the problem at hand.

    So the "mask" is really a bitwise trick of sorts? I need to use it as an intermediate step to get the bits I want, right?

    So for bits 5 and 6:

    char a_char = 01101010;

    char my_mask = 00110000;

    char result = 00100000;

    and then use the result as needed (keeping in mind that if I end up with 0 at 5 or 6 I had a 0 to begin with, and a 1 to begin with for a result of 1)

    I think I get the idea now.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    okay, cool. so's I'd like to get bits 5 and 6 of a char, and modify them (because these bits control which grouping of 32 chars I'm in). Bear with me, this is all part of a program I'm writing.
    To extract:

    Code:
    val = (x & 0x30) >> 4;
    To store:

    Code:
    x = (x & ~0x30) | (val << 4)

  11. #11
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    okay thank you all, I've still got more to learn about this but at least I've got a starting point now.

    Don't get angry though, I don't understand the syntax you guys are using with:

    0X30
    0X10
    etc.

    How do those represent a specific bit within a byte?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They are numbers in hexadecimal representation. Makes it easier to convert to binary, e.g.,
    0x30
    0011 0000

    since 3_16 = 11_2 and 0_16 = 0_2, and of course 0x30 = 3 * 16 + 0 = 48.
    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

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    thanks, so does the 0x refer to the sign of the number, or is it simply denoting that it's in hex?

    should i assume two's complement then? i've heard that's processor specific though...

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    thanks, so does the 0x refer to the sign of the number, or is it simply denoting that it's in hex?
    Simply notation.

    should i assume two's complement then? i've heard that's processor specific though...
    It will be either one's or two's complement, but exactly which is implementation defined.
    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

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    should i assume two's complement then? i've heard that's processor specific though...
    The complement has to do with sign and arithmetic. Bitwise operations don't care about the complement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM