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 7840707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:53:01+00:00 2026-06-02T15:53:01+00:00

How would one set/unset enum value for something like the following. Using gcc, I

  • 0

How would one set/unset enum value for something like the following. Using gcc, I get this annoying warning:

test.c:37: warning: negative integer implicitly converted to unsigned type
test.c:39: warning: negative integer implicitly converted to unsigned type
test.c:41: warning: negative integer implicitly converted to unsigned type
test.c:43: warning: negative integer implicitly converted to unsigned type

code is:

#include <stdio.h>
#include <string.h>

typedef enum {
 ONE = 0x1,
 TWO = 0x2,
 THREE = 0x4,
 FOUR = 0x8,
} options;

static const char *byte_to_binary (int x)
{
  int z;
  static char b[9];
  b[0] = '\0';

  for (z = 256; z > 0; z >>= 1)
    {
    strcat(b, ((x & z) == z) ? "1" : "0");
    }

  return b;
}

int main(int argc, char *argv[])
{
  options o = 0;
  printf( "%s\n", byte_to_binary(o));
  o |= ONE;
  printf( "%s\n", byte_to_binary(o));
  o |= TWO;
  printf( "%s\n", byte_to_binary(o));
  o |= THREE;
  printf( "%s\n", byte_to_binary(o));
  o |= FOUR;
  printf( "%s\n", byte_to_binary(o));
  o &= ~FOUR;
  printf( "%s\n", byte_to_binary(o));
  o &= ~THREE;
  printf( "%s\n", byte_to_binary(o));
  o &= ~TWO;
  printf( "%s\n", byte_to_binary(o));
  o &= ~ONE;
  printf( "%s\n", byte_to_binary(o));

  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-06-02T15:53:02+00:00Added an answer on June 2, 2026 at 3:53 pm

    Since your enum does not contain any negative integer constants, I guess GCC has given unsigned int type to your enums. Now the expressions like

    o &= ~FOUR
    

    are equivalent to

    o = o & ~FOUR
    

    On the RHS, o is unsigned int and ~FOUR is signed int and by type conversion rules, signed int will be converted to unsigned int. Also ~FOUR is a negative number and hence you get a warning for implicit conversion of a negative number to unsigned type.

    If you are sure of your logic, you need not worry about the warnings or you can convert your enum to signed by having a dummy enum which equals to a negative number.

    Something like

    typedef enum {
     DUMMY =-1,
     ONE = 0x1,
     TWO = 0x2,
     THREE = 0x4,
     FOUR = 0x8,
    } options;
    

    Also, your code has run-time buffer overflow problems. In function byte_to_binary you are checking for 9 bits, but your buffer is also 9 bytes. It has to be 10 bytes, one for the terminating null. Make it static char b[10]; and everything works fine

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

Sidebar

Related Questions

I would like to set some vim options in one file in the comments
how would one do an IPad like overlay window like this in WPF with
How does one set a default value for a text input using ActiveScaffold 1.2RC1?
How would one adapt this jquery plugin so that it counts down from how
How would one create a Singleton class using PHP5 classes?
How would one set a minimum interval for a launchd.plist script with a WatchPath
I would like to add button myButton to TextView. How to get it in
How would one set up an email alert to be sent, if there are
Say i have the following: public class Person{ public int ID {get; set;} public
Why would one set the Style property of a control to a template binding

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.