1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

bit manipulation in c

Discussion in 'C' started by DeepikaNS, Apr 20, 2010.

  1. DeepikaNS

    DeepikaNS New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    we have a buffer say char array.[1].so the size will be 1 byte.
    Now our problem is say we have 8 bits data say 10110101.
    now this must be assigned to array[0] variable as 1 byte.
    bit manipulation is not happening.kindly help us in this regard
    If possible post us a sample code for the same

    here we are posting sample code for the same



    1. decleration:
    2. struct aaa {
    3. unsigned char buff1:1;
    4. unsigned char buff2:1;
    5. unsigned char buff3:1;
    6. unsigned char buff4:1;
    7. unsigned char buff5:1;
    8. unsigned char buff6:1;
    9. unsigned char buff7:1;
    10. unsigned char buff8:1;
    11. }complete_buff;
    12. complete_buff->buf1=(unsigned char)0x01;;
    13. complete_buff->buf2=(unsigned char)0X00;
    14. complete_buff->buff3=(unsigned char)0x01;
    15. complete_buff->buff4=(unsigned char)0x01;
    16. complete_buff->buff5=(unsigned char)0x00;
    17. complete_buff->buff6=(unsigned char)0x00;
    18. complete_buff->buff7=(unsigned char)0x00;
    19. complete_buff->buff8=(unsigned char)0x00;
    20. char array[0]=(complete_buff->buff1>>7)| (complete_buff->buff2>>6)|(complete_buff->buff3>>5)|(complete_buff->buff4>>4)|(complete_buff->buff5>>3)|(complete_buff->buff6>>2)|(complete_buff->buff7>>1)|(complete_buff->buff8)
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    convert bits into dec value and then store the ascii character of this value into your array
    (for example 00110001(bin)-->49(dec)--->'1'(ascii char) )

    Code:
    ..........
        char a=49;
        printf("\n a=%c",a);
    ..............
    
    check here for code related to convert bin to dec
    http://www.daniweb.com/code/snippet216372.html#
     
    Last edited: Apr 20, 2010
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The posted code (by the OP) doesn't work because you're shifting the wrong way. If you shift 00000001 right by any number of bits this will become zero. To get 10000000 from 00000001 you need to shift it LEFT by 7 bits (operator <<), not right (>>).
     

Share This Page