Bit manipulation across 2 bytes?

This is a discussion on Bit manipulation across 2 bytes? within the C Programming forums, part of the General Programming Boards category; I have read up on bit manipulation and understand it fine i.e. How to set certain bits, get certain bits, ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    Bit manipulation across 2 bytes?

    I have read up on bit manipulation and understand it fine i.e. How to set certain bits, get certain bits, shift bits etc etc.

    However what if the bits are across two bytes? e.g.

    Code:
    char a = 255;
    char b;
    char c;
    'Char a' is fine as it is handled in one bit. I understand how to work with bits like this.

    However, what if I want to assign 400 to characters b and c? Is there a simple way?

    I'm a bit confused as you may be able to tell but would appreciate any suggestions.

    Thanks in advance

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    First off you can not assign to a char type like that. You must do something like

    Code:
       char a = 'z';
    To bit shift in either direction, as all data is stored binary, are you familar with left and right shift bit operators? '<<' and '>>'

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by slingerland3g
    First off you can not assign to a char type like that.
    You can, although it may not be advisable if you do not know if the integer literal will fit into a char.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by laserlight View Post
    You can, although it may not be advisable if you do not know if the integer literal will fit into a char.
    True, strike that (at least up to a value of 127 for char). Once you know you have assigned to 'a' properly, within its data storage, then you can do something like:

    Code:
     ...
       char b = a >> 1;
     ...
    By using bit shifting you are essentially dividing(right shift) or multiplying(left shift)
    Last edited by slingerland3g; 12-03-2009 at 11:14 AM.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    yes I'm familiar with the bit shift operators. Will this help?

    As laserlight says it is possible to assign this way, I have this part working in a test program. I just can't operate on two bytes i.e. a "word".

    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Well, in the case of two bytes, you could just use say, an unsigned short, if an unsigned short has a size of two bytes (and it probably does).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Thanks for your reply.

    In the application I am using it within I have to use a char array to store my data.

    The first two characters are required to hold an 'ID' field.

    So I need some way of assigning this and can't seem to find a solution.

    All I can think of so far I breaking it down into sub strings. Then i'd have something like:

    Code:
    char id[2];
    
    id = 400;
    The append all my substrings together to create my array of length 12. Would this work in theory?

    Thanks again.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Yeah you are running into data size limits. As laserlight suggested an unsigned short will do the trick at least up to a decimal value of 32768 if left shift.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Martin_T View Post
    Thanks for your reply.

    In the application I am using it within I have to use a char array to store my data.

    The first two characters are required to hold an 'ID' field.

    So I need some way of assigning this and can't seem to find a solution.

    All I can think of so far I breaking it down into sub strings. Then i'd have something like:

    Code:
    char id[2];
    
    id = 400;
    The append all my substrings together to create my array of length 12. Would this work in theory?

    Thanks again.

    A char can only hold up to a value of 127. You are trying to assign a value of 400. This will not work as noted already. I think I see where you are going with this though, and what you need to do is divide your two bytes and split them across two char data types to store this. Is that correct?

    Perhaps the below link will help you figure out the data size of your identifiers you need to use for this.

    C Guide--1.2 Indentifiers

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    I think you are misunderstanding his problem. The question is, given two bytes b and c, how can you use those 16 bits to store the value 400?

    First, use unsigned char, not char. The sign bit just gets in the way in these sorts of calculations. Assuming little-endian (Intel) representation, the lower 8 bits of the value 400 will go into b, and the upper 8 bits into c:

    Code:
    unsigned char b = 400 & 0xFF;
    unsigned char c = ( 400 >> 8 ) & 0xFF;
    Bit manipulation is done by combinations of shifting, setting, and masking using the operators <<, >>, &, |, and ~.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    You can use a union, its just these kind of purposes its ment for:

    Code:
    typedef union
    {
    	unsigned char b[2];
    	unsigned short c;
    } byteArr_t;
    
    int main(void)
    {
    	byteArr_t a;
        
    	a.c = 400;
    
    	printf("Result: %d %d %d\n", a.b[0], a.b[1], a.c);
    	
    	a.b[0] = 55;
    	a.b[1] = 66;
    
    	printf("Result: %d %d %d\n", a.b[0], a.b[1], a.c);
    
    	getc(stdin);
    
    	return 0;
    }

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by brewbuck View Post
    I think you are misunderstanding his problem. The question is, given two bytes b and c, how can you use those 16 bits to store the value 400?

    First, use unsigned char, not char. The sign bit just gets in the way in these sorts of calculations. Assuming little-endian (Intel) representation, the lower 8 bits of the value 400 will go into b, and the upper 8 bits into c:

    Code:
    unsigned char b = 400 & 0xFF;
    unsigned char c = ( 400 >> 8 ) & 0xFF;
    Bit manipulation is done by combinations of shifting, setting, and masking using the operators <<, >>, &, |, and ~.


    Not sure that was directed at me, from what you coded, that was my idea as well. Though my mask was set to 255 in my mind. I just did not want to actually code this for them. I wanted to stress the fact that they were exceeding their char data size with assigning 400 to char. So ultimately if they assign their byte values -- ID[0] will have the value of 1 and ID[1] will have the value 144, just so we are all on the same page.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Quote Originally Posted by slingerland3g View Post
    Not sure that was directed at me, from what you coded, that was my idea as well. Though my mask was set to 255 in my mind. I just did not want to actually code this for them. I wanted to stress the fact that they were exceeding their char data size with assigning 400 to char. So ultimately if they assign their byte values -- ID[0] will have the value of 1 and ID[1] will have the value 144, just so we are all on the same page.
    Thanks for the reply.

    I thought you had misunderstood me also as I knew 400 was outside the char data limit, that was my reason for posting. Also I think you mistaken in a previous post about a char only being able to hold 127? 8 bits = up to 255 if i've got my binary correct?

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Quote Originally Posted by brewbuck View Post
    I think you are misunderstanding his problem. The question is, given two bytes b and c, how can you use those 16 bits to store the value 400?

    First, use unsigned char, not char. The sign bit just gets in the way in these sorts of calculations. Assuming little-endian (Intel) representation, the lower 8 bits of the value 400 will go into b, and the upper 8 bits into c:

    Code:
    unsigned char b = 400 & 0xFF;
    unsigned char c = ( 400 >> 8 ) & 0xFF;
    Bit manipulation is done by combinations of shifting, setting, and masking using the operators <<, >>, &, |, and ~.
    Thankyou! This is what I was trying to say although I think maybe I was a little unclear. I am coding for big-endian but that's easily altered now I understand how this is done.

    You help is appreciated!

    Thanks once again

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Martin_T View Post
    Thanks for the reply.

    I thought you had misunderstood me also as I knew 400 was outside the char data limit, that was my reason for posting. Also I think you mistaken in a previous post about a char only being able to hold 127? 8 bits = up to 255 if i've got my binary correct?
    A signed char can take on 255 distinct values in the range -128 to 127. 255 isn't in that range.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit manipulation
    By dsupriya in forum C Programming
    Replies: 8
    Last Post: 03-05-2009, 08:36 AM
  2. Reverse Engineering on a Download file
    By c_geek in forum C Programming
    Replies: 1
    Last Post: 03-22-2008, 03:15 PM
  3. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 07:22 PM

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