char to bits representation

This is a discussion on char to bits representation within the C Programming forums, part of the General Programming Boards category; Hi All Sorry for basic question but i am new to this problem. I search alot but could not succeed. ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    char to bits representation

    Hi All
    Sorry for basic question but i am new to this problem. I search alot but could not succeed.

    I have 3x3 char array
    1,1,0
    0,0,0
    0,1,0

    when i store them in an image using libtiff library on Linux OS,left most 1 appears on 8th position and second 1 appears on 16th position.

    I need them on first 2 positions as they are instead they are on 8th and 16th.

    How can i convert the above array to bits from chars so that it appears as it is, when i store in image.

    Thanks for help

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    that's because a character, or unsigned character is represented by 8 bits. I think what you're after is 11000000 (binary) = 192 (decimal).

    What you have is 1,1,0 (decimal) = 00000001, 00000001, 00000000 (binary)

    EDIT: you could always bit shift the value 1 into a character/unsigned character. That would be another way of achieving 11000000.
    Last edited by scwizzo; 03-15-2010 at 06:28 PM.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Have a look at the bit masking on the internet, it will show you on how to set and unset bits in a given btye.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 03:31 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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