Accessing/modifiying int's HIBYTE?

This is a discussion on Accessing/modifiying int's HIBYTE? within the C Programming forums, part of the General Programming Boards category; Lets say I have an INT (INT yyz; ) and I what to put 44 in the HIBYTE location WITHOUT ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    87

    Accessing/modifiying int's HIBYTE?

    Lets say I have an INT (INT yyz; ) and I what to put 44 in the HIBYTE location WITHOUT using a pointer to reference yyz HIBYTE....Meaning NOT using: INT tnt = HIBYTE(yyz);. How might this be possible?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Assuming a 32-bit int, you might do something like:
    Code:
    yyz = (yyz & 0xFFFFFF) | (44 << 24);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Your example "INT tnt = HIBYTE(yyz);" doesn't actually use a pointer. All it's doing is bit manipulation.

    Assuming your int is 4 bytes, you need to do something like
    yyz = (yyz & 0x00FFFFFF) | 44 << 24;

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Quote Originally Posted by anduril462 View Post
    Your example "INT tnt = HIBYTE(yyz);" doesn't actually use a pointer. All it's doing is bit manipulation.

    Assuming your int is 4 bytes, you need to do something like
    yyz = (yyz & 0x00FFFFFF) | 44 << 24;
    Heh.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by TAZIN View Post
    Lets say I have an INT (INT yyz; ) and I what to put 44 in the HIBYTE location WITHOUT using a pointer to reference yyz HIBYTE....Meaning NOT using: INT tnt = HIBYTE(yyz);. How might this be possible?
    Using pointers is much more portable, actually (works for any size int):

    Code:
    #define HIBYTE( value ) *( ( ( unsigned char* )&value ) + ( sizeof( value ) - 1 ) )
    
    int main( void )
    {
        short s = 0x03ff;
        int i = 0x03ffffff;
        printf( "%d\n", HIBYTE( s ) );
        printf( "%d\n", HIBYTE( i ) );
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Quote Originally Posted by Sebastiani View Post
    Using pointers is much more portable, actually (works for any size int):

    Code:
    #define HIBYTE( value ) *( ( ( unsigned char* )&value ) + ( sizeof( value ) - 1 ) )
    
    int main( void )
    {
        short s = 0x03ff;
        int i = 0x03ffffff;
        printf( "%d\n", HIBYTE( s ) );
        printf( "%d\n", HIBYTE( i ) );
    }
    Of course, now you've introduced endian issues.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by itsme86 View Post
    Of course, now you've introduced endian issues.
    It's late, okay?

    Code:
    unsigned char HIBYTE_IMPL_( void* ptr, size_t siz )
    {
        static unsigned long tst = 1;
        static int lil = *( unsigned char* )&tst == 1;
        size_t off = lil ? siz - 1 : 0;    
        return *( ( unsigned char* )ptr + off );
    }
    
    #define HIBYTE( value ) HIBYTE_IMPL_( &value, sizeof( value ) )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_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;
    }

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Is it safe to say the same approach can be taken when working with HIWORD? Also, how would HIBYTE subtraction be handled? Let's say an INT has 16 in the HIBYTE and 28 in the LOBYTE, and I want to subtract 12 from the HIBYTE and not alter the LOBYTE while all along not using an additional variable. How would this be tackled?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Also, note that the OP specifically said:
    Quote Originally Posted by TAZIN View Post
    WITHOUT using a pointer to reference yyz

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Code:
    BYTE lo, hi;
    lo = LOBYTE(x);
    hi = HIBYTE(x) - 12;
    x = hi << 8 | lo;
    EDIT: Note that this is assuming x is 2 bytes. The principle is the same for larger data types.
    Last edited by anduril462; 12-09-2010 at 05:27 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TAZIN View Post
    Is it safe to say the same approach can be taken when working with HIWORD? Also, how would HIBYTE subtraction be handled? Let's say an INT has 16 in the HIBYTE and 28 in the LOBYTE, and I want to subtract 12 from the HIBYTE and not alter the LOBYTE while all along not using an additional variable. How would this be tackled?
    Is there a reason you don't just shift the 12 over and be done with it?

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    "tabstop"
    Could you please elaborate just a bit.....Remember, I'm just a novice so handle with kid gloves.

    Currently, I'm doing something similiar to what "anduril462" stated:

    Code:
     c = (yyz >> 8) - 12;
    c = 12;
    I'd like to be able to do the HIBYTE subtraction WITHOUT using the additional variable INT c; .

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TAZIN View Post
    "tabstop"
    Could you please elaborate just a bit.....Remember, I'm just a novice so handle with kid gloves.

    Currently, I'm doing something similiar to what "anduril462" stated:

    Code:
     c = (yyz >> 8) - 12;
    c = 12;
    I'd like to be able to do the HIBYTE subtraction WITHOUT using the additional variable INT c; .
    So don't use it.
    Code:
    your_original_variable -= 12 << 8;
    Shift the 12 into position, then subtract it away.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Perhaps tabstop's method then. It would be something like:
    Code:
     x = x - (12 << 8);
    The only downside to this is it could potentially affect LOBYTE if HIBYTE is smaller than 12. Maybe this instead:
    Code:
    x = ((HIBYTE(x) - (12 << 8)) | LOBYTE(x)

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by anduril462 View Post
    The only downside to this is it could potentially affect LOBYTE if HIBYTE is smaller than 12. Maybe this instead:
    Code:
    x = ((HIBYTE(x) - (12 << 8)) | LOBYTE(x)
    Subtraction borrowing doesn't go to the right, as I've ever used it. The lobyte will still have the same value (in and of itself) as before. The int may turn negative/get really large (if it's unsigned).

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

Similar Threads

  1. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2010, 09:45 AM
  2. Dynamically allocate 2D array of ints
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 10-24-2009, 08:31 PM
  3. how to handle ints and strings when writing to file
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 04-23-2008, 04:44 AM
  4. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 05:12 AM
  5. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 08:42 PM

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