Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:07:47+00:00 2026-05-26T22:07:47+00:00

The following way of checking for the signed number representation checks for twos complement

  • 0

The following way of checking for the signed number representation checks for twos complement correctly on my machine, but I dont have ones complement or signed magnitude machines to check it. Would the code work properly and more importantly, is it portable?

File: platform.h

#ifndef PLATFORM_H
#define PLATFORM_H
#include <limits.h>

static
const union {
    signed char sc;
    unsigned char uc;
} plat_4xvYw = {.sc = -1};

#define IS_TWOS_COMPL (plat_4xvYw.uc == UCHAR_MAX)
#define IS_ONES_COMPL (plat_4xvYw.uc == UCHAR_MAX - 1)
#define IS_SIGNED_MAG (plat_4xvYw.uc == (1U << (CHAR_BIT - 1)) + 1U)

#endif

File: a.c

#include <inttypes.h>
#include <limits.h>
#include "platform.h"
#include <assert.h>

int
main (void) {

    assert (IS_TWOS_COMPL);
    if (IS_TWOS_COMPL) {

        printf ("twos complement\n");
    } else if (IS_ONES_COMPL) {

        printf ("ones complement\n");
    } else if (IS_SIGNED_MAG) {

        printf ("signed magnitude\n");
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:07:48+00:00Added an answer on May 26, 2026 at 10:07 pm

    I think you’re better off just masking the bits of a negative int:

    if ((-1 & 0x1) == 0) {
        // -1 ends in "0" => 1s' complement
    } else if ((-1 & 0x2) == 0) {
        // -1 ends in "01" => sign-magnitude
    } else {
        // -1 ends in "11" => two's complement
    }
    

    Strictly speaking, this doesn’t tell you the same thing as your code, since there’s no guarantee that int and signed char use the same meaning of the sign bit. But (a) seriously? and (b) this works for types int and larger, for smaller types it’s trickier. unsigned char is guaranteed to have no padding bits, but signed char is not. So I think it’s legal to have (for example) CHAR_BIT == 9, UCHAR_MAX = 511, CHAR_MAX = 127, and signed char has 1 padding bit. Then your code could fail: the sign bit in the stored signed value isn’t necessarily where you expect it to be, and the value of the padding bit could be either 0 or 1.

    In a lot of cases you could just use int8_t in the program instead of signed char. It’s guaranteed to be 2’s complement if it exists, so might save you from caring about the representation of signed char. If it doesn’t exist, the program won’t compile, which is kind of what you’re asserting anyway. You’d get a false negative from platforms which are 2’s complement, but don’t have an 8-bit char and therefore do not provide int8_t. This may or may not bother you…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm checking it in the following way: if (Environment.UserInteractive) Application.Run(new ServiceControllerForm(service)); else ServiceBase.Run(windowsService);
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
Hey; I'm currently using the following way of checking whether an image exists on
I want to set registry keys in following way if OS is Vista then
I set a header in the following way: header('Content-Length: ' . filesize($strPath)); On my
I've been using ShowDialog() in following way for all my new Windows. SomeGui test
I try to do it in the following way: public String getValue(String service, String
I am trying to specialize template the following way: template<size_t _1,size_t _2> // workaround:
I often do code review in the following way: Open the SVN log Select
I'm trying to serialize a Type object in the following way: Type myType =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.