how is this evaluated?

This is a discussion on how is this evaluated? within the C++ Programming forums, part of the General Programming Boards category; if(size & 1)...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    how is this evaluated?

    if(size & 1)

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Assuming 'size' is an integral type, it is the bitwise AND, and returns a 1 for every position where the two integers both have a 1, and 0 otherwise.

    That, therefore, will only return true when the right-most bit is 1.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    if (size_is_odd)
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    im beginning to understand the value of understanding binary...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Stoned_Coder
    Code:
    if (size_is_odd)
    Just out of curiosity, will it be the same on big-endian machines?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    yeah it would, because 1 would be stored like this in binary:

    0001 0000

    so it would still work

    edit: assuming 8 bits in a byte. I think it's a pretty safe assumption.
    Away.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Isn't endian-ness transparent as long as you don't use pointers?
    Even for shifts?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The tricks were what I meant with pointers.

    Code:
    // writes a big-endian double word to a memory location
    static void put_be_dword(byte *target, dword dw) {
    	*target++ = static_cast<byte>(dw >> 24);
    	*target++ = static_cast<byte>(dw >> 16);
    	*target++ = static_cast<byte>(dw >>  8);
    	*target++ = static_cast<byte>(dw      );
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Endianness is also an issue when reading binary data files and the like.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's what my code was for - reading PNG files...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by blackrat364
    yeah it would, because 1 would be stored like this in binary:

    0001 0000

    so it would still work
    Thanks. Thats what I thought, I wasn't quite sure though.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by blackrat364
    yeah it would, because 1 would be stored like this in binary:

    0001 0000

    so it would still work

    edit: assuming 8 bits in a byte. I think it's a pretty safe assumption.
    Big endian/little endian doesn't alter the layout of a byte, it affects the layout of multiple bytes forming words.

    E.g. in hex, 8 as a 32-bit little endian (four 8-bit bytes) is "08 00 00 00", in big endian, "00 00 00 08".

    By definition, you can't reorganize the bits in a byte anyway -- a byte by definition is the smallest addressable location in memory. Bits have a logical order (most significant bit .. least significant bit) but not a physical order (i.e. a bit has no address). So there IS no "order" that bits are stored in memory, there is only the logical order of the bits themselves.

    On the other hand, words made of multiple bytes have a logical order (most significant byte, second most significant byte, etc.) and a physical order (addresses). Because they have both a logical and physical order, there are 2 ways to store a sequence of bytes:

    In order from smallest to largest physical address, you can have:
    [Most significant byte] [Second most significant byte] ... [Least Significant Byte] <= This is "big endian"
    [Least significant byte] [Second least significant byte] ... [Most Significant Byte] <= This is "little endian"

    "Endian"ness describes the way that bytes within words are mapped between their logical and physical orders. Each has advantages and disadvantages. Big endian is more human-readable; little-endian makes code for conversion, etc. conceptually simpler. Both are used; Motorola prefers big endian, Intel little endian.
    Last edited by Cat; 06-18-2003 at 02:04 PM.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just assume those numbers are each a byte - the logic still holds. Even though it is a little endian setup.
    Last edited by Zach L.; 06-18-2003 at 01:45 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The logic does, but it might be confusing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Alrighty then... Presenting the various formats of 1 (as expressed in a 32 bit integer):

    Code:
    memory address : 0    1    2    3
    bid endian     : 0x00 0x00 0x00 0x01
    little endian  : 0x01 0x00 0x00 0x00
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding character arrays to strings.
    By w00tw00tkab00t in forum C++ Programming
    Replies: 28
    Last Post: 02-06-2006, 06:03 PM
  2. i++ query
    By PING in forum C Programming
    Replies: 29
    Last Post: 11-21-2004, 12:40 PM
  3. problem with letters
    By librab103 in forum C Programming
    Replies: 3
    Last Post: 08-08-2003, 01:54 PM
  4. Expression cannot be evaluated
    By *pointer in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 10:17 AM
  5. help! - 'cxx0030:expression cant be evaluated.'
    By voydangel in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2001, 02:32 AM

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