Divisible by 2

This is a discussion on Divisible by 2 within the C Programming forums, part of the General Programming Boards category; Originally Posted by cpjust Why would it be any different for signed numbers? As was already stated, if 11111110 is ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by cpjust View Post
    Why would it be any different for signed numbers?
    As was already stated, if 11111110 is -1 for a byte, this will be read as an even number.

    It will work on signed numbers if 2's complement is used.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Why would it be any different for signed numbers?
    If it's a ones complement, negative numbers will be "even when they are odd". Of course the machines that use ones complement also have a "negative zero" value, and I haven't ever worked on a machine with ones complement for negative numbers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Seeing as bit 0 is the sign bit, perhaps the bit will be shifted to the 3rd position instead?
    I don't know, but it sounds logical.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elysia View Post
    Seeing as bit 0 is the sign bit, perhaps the bit will be shifted to the 3rd position instead?
    I don't know, but it sounds logical.
    I'm confused again. I thought bit 31 (on a 4-byte integer) was usually the sign bit. And, why does that matter? And ... 3rd position? And ... shifting? Why are we shifting?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Seeing as bit 0 is the sign bit, perhaps the bit will be shifted to the 3rd position instead?
    I don't know, but it sounds logical.
    Huh? Bit zero isn't the sign bit in any type of integers that I'm aware of. Ones complement still use the top bit for sign, but it uses a direct "inverse" of the positive number, instead of "inverse+1" as two's complement use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    My bad. It's the last bit (31th for 4 bytes), not the first (bit 0).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MacGyver View Post
    As was already stated, if 11111110 is -1 for a byte, this will be read as an even number.

    It will work on signed numbers if 2's complement is used.
    Oh crap, I think you're right. I don't know much about those 1's vs 2's complement numbers, but I've only worked on machines that use 11111111 as -1.
    What platforms use the other complement for numbers?

  8. #23
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by matsp View Post
    It works on all machines that use two's complement for negative numbers, but if your -1 value is 1111....1110 then the and with 1 will produce zero for -1, which is of course incorrect.
    Yeah, things get more confusing with negative values. But even the mod operator '%' can be confusing when applied to negative quantities.

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

Similar Threads

  1. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 01:50 PM
  2. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  3. Simple C program displaying all numbers divisible by 6
    By DaniiChris in forum C Programming
    Replies: 25
    Last Post: 07-06-2008, 12:25 PM
  4. Help!!
    By ryang in forum C++ Programming
    Replies: 6
    Last Post: 11-20-2006, 11:45 PM
  5. C programming
    By annamayya in forum C Programming
    Replies: 20
    Last Post: 11-20-2004, 01:42 AM

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