Like Tree4Likes

Accesing individual bytes in an int

This is a discussion on Accesing individual bytes in an int within the C Programming forums, part of the General Programming Boards category; Which is a faster way to access the four individual bytes that make up an int: 1) Have a pointer ...

  1. #1
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32

    Accesing individual bytes in an int

    Which is a faster way to access the four individual bytes that make up an int:

    1) Have a pointer to the integer, cast it to a pointer to char and access the individual bytes through this pointer - *ptr, *(ptr+1), *(ptr+2), *(ptr+3)

    2) Using bitwise operators >>, &

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    This smells like a homework question. Even if it isn't, why don't you take a crack at it yourself. Tell us what you think the answer is and why. We'll let you know whether you're right, and explain it if you're wrong.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    What do you think?
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by PING View Post
    What do you think?
    I personally think the bitwise operators are faster. And no, it's not a homework question. :-)

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You forgot to answer the "and why" part of my question. Something that shows me you thought about it rather than simply picked option 2. So why do you think bitwise operators are faster?

  6. #6
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by anduril462 View Post
    You forgot to answer the "and why" part of my question. Something that shows me you thought about it rather than simply picked option 2. So why do you think bitwise operators are faster?
    Because bitwise operations are part of the CPU's instruction set? Because I need to access memory more often when using option 1?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    3. Use an Union

    Number 3 is my guess. Because that is the way I learned to do it.

    Tim S.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Or try this one.
    Bitwise always works, and mucking about with pointers/unions exposes you to endian issues on the processor architecture.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    Quote Originally Posted by Salem View Post
    Or try this one.
    Bitwise always works, and mucking about with pointers/unions exposes you to endian issues on the processor architecture.
    I agree Bitwise is better because it is more portable; not sure if it is faster.
    But, the difference in time is likely to be too small to care about in most cases.

    Tim S.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    I'll take fast (and always works) vs. probably just as fast (and WTF's happening here!?)
    claudiu likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This is a pretty silly question to begin with as Salem pointed it out because you can't produce an exact answer for any possible real or theoretic CPU architecture and it is hard to believe that in this day and time the difference in performance would have any effect on anything. I too, go for option 2)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    As for the endian issues, yes, the bitwise operators avoid them. There is one caveat with the >> operator though. If you're using signed ints (your OP didn't specify), you will end up with implementation-defined behavior if that int has a negative value (or undefined behavior if you use a << on a negative, signed int). That is why you must always do your bit shifting operations on unsigned integers, or cast the number first. At least a cast doesn't cost any clock cycles.

    As for the actual question, it is a bit silly. You probably aren't writing code for which this speed difference matters. Besides, it depends greatly on the architecture and other factors. You may be working on an architecture that is particularly well- or poorly-suited for bitwise operations. You may not have efficient addressing modes for the pointer method, or you may have poor or no caching, making the pointer go all the way out to main memory for each one. The compiler may generate poor code for one scenario, or be able to heavily optimize one method but not the other.

    I would prefer the bitwise operations, remembering to only operate on unsigned ints.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I vote for option #1. Let the compiler generate the necessary bitwise operations itself, if it has to. Likely it will find single-byte access instructions and manipulate memory areas directly without shifting.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Four memory accesses is a hell of a lot worse than one memory access.
    phantomotap likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    Four memory accesses is a hell of a lot worse than one memory access.
    How are you getting one memory access?
    Code:
    unsigned int fourbytes;
    unsigned char b0, b1, b2, b3;
    
    b0 = 0xFF & (fourbytes); /* one memory access */
    b1 = 0xFF & (fourbytes ... shifted ); /* two memory accesses */
    b2 = 0xFF & (fourbytes ... shifted ); /* three memory accesses */
    b3 = 0xFF & (fourbytes ... shifted ); /* four memory accesses */

    Quzah.
    Hope is the first step on the road to disappointment.

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

Similar Threads

  1. help in accesing .txt
    By winber in forum C Programming
    Replies: 5
    Last Post: 12-04-2010, 10:28 AM
  2. working set, virtual bytes and private bytes
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2008, 01:39 AM
  3. Reading Individual bits from 2 bytes
    By Giania in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 02:16 AM
  4. Accesing Windows
    By MethodMan in forum Linux Programming
    Replies: 3
    Last Post: 06-04-2002, 12:14 AM
  5. accesing memory
    By Prakash in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 02:46 PM

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