copy some bits into a 8 bits binary number

This is a discussion on copy some bits into a 8 bits binary number within the C Programming forums, part of the General Programming Boards category; How can I copy several bits into a binary number or how can I reset some bits with one command ...

  1. #1
    Unregistered
    Guest

    Question copy some bits into a 8 bits binary number

    How can I copy several bits into a binary number or how can I reset some bits with one command ?

    I can select a section from the number with original number & ~ (~0 << length of section )

    Thanks
    Tepy

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    You can OR them into the number with this '|=' and extract them with '&='.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    For example resetting bit 1 of a byte:

    /* Define a mask */
    #define RESET_BIT_1 0xF2 /* 1111:1101 */

    unsigned char byte;

    byte &= RESET_BIT_1;

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    2

    Wink

    Thanks the ideas, I try them.

    Tepy

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    2

    Unhappy

    Unfortunately I can't use this for my problem. I need some ideas for example how I can change some bits in a bitstream.
    For examle the source bitstream is 11100111, the sample bits let 100. I want to change the source bitstream's second, third and fourth bit with 100.

    Thanks
    Tepy

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Then you could consider using bitfields.

    Code:
    /* Define a bitfield */
    typedef struct
    {
        unsigned int b0:1;
        unsigned int b1:1;
        unsigned int b2:1;
        unsigned int b3:1;
    
        unsigned int b4:1;
        unsigned int b5:1;
        unsigned int b6:1;
        unsigned int b7:1;
    } bitfield_s;
    
    void function ()
    {
        unsigned char *var1;
        bitfield_s *var2;
     
        /* The bitstream */
        *var1 = 0xF0;    
        
        printf ("%02X\n", *var1);
        
        /* Map it on a bitfield */           
        var2 = (bitfield_s *) var1;
        
        /* Manipulate bits */
        var2->b0 = 1;
        var2->b1 = 1;
        var2->b2 = 1;
        var2->b3 = 1;
        var2->b4 = 0;
        var2->b5 = 0;
        var2->b6 = 0;
        var2->b7 = 0;
        
        /* Put it back */
        var1 = (unsigned char *) var2;
        
        printf ("%02X\n", *var1);
    }
    More easier than mapping is the use of a union. Like

    Code:
    typedef union
    {
        bitfield_s bitfield;
        unsigned char byte;
    } byte_u;
    
    void function (void)
    {
        byte_u var1;
        
        var1.byte = 0xF0;
        
        printf ("%02X\n", var1.byte);    
       
        /* Manipulate bits */
        var1.bitfield.b0 = 1;
        var1.bitfield.b1 = 1;    
        var1.bitfield.b2 = 1;    
        var1.bitfield.b3 = 1;    
        var1.bitfield.b4 = 0;    
        var1.bitfield.b5 = 0;    
        var1.bitfield.b6 = 0;    
        var1.bitfield.b7 = 0;    
       
        printf ("%02X\n", var1.byte);
    }

  7. #7
    *
    Guest
    > Unfortunately I can't use this for my problem. I need some ideas for example how I can change some bits in a bitstream.

    Of course you can use it for a bitstream. This is called bitwise manipulation and is how you do it in the most effecient manner.

    You create a mask for each bit and then manipulate each bit using the value and the mask.

    Go to the library, or the bookstore, and look at 'bitwise' or 'bit manipulation' in one of their chapters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. What's problem of adding two binary number?
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 01-12-2007, 05:12 AM
  3. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 09:39 PM
  4. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21