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

  • SEARCH
  • Home
  • 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 1014411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:15:01+00:00 2026-05-16T10:15:01+00:00

One thing I’ve never really understood is why in many libraries, constants are defined

  • 0

One thing I’ve never really understood is why in many libraries, constants are defined like this:

public static final int DM_FILL_BACKGROUND = 0x2;
public static final int DM_FILL_PREVIOUS = 0x3;
public static final int TRANSPARENCY_MASK = 1 << 1;
public static final int TRANSPARENCY_PIXEL = 1 << 2;

What’s up with the 0x and << stuff? Why aren’t people just using ordinary integer values?

  • 1 1 Answer
  • 3 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-16T10:15:01+00:00Added an answer on May 16, 2026 at 10:15 am

    The bit shifting of 1 is usually for situations where you have non-exclusive values that you want to store.

    For example, say you want to be able to draw lines on any side of a box. You define:

    LEFT_SIDE   = 1 << 0  # binary 0001 (1)
    RIGHT_SIDE  = 1 << 1  # binary 0010 (2)
    TOP_SIDE    = 1 << 2  # binary 0100 (4)
    BOTTOM_SIDE = 1 << 3  # binary 1000 (8)
                                   ----
                                   0111 (7) = LEFT_SIDE | RIGHT_SIDE | TOP_SIDE
    

    Then you can combine them for multiple sides:

    DrawBox (LEFT_SIDE | RIGHT_SIDE | TOP_SIDE) # Don't draw line on bottom.
    

    The fact that they’re using totally different bits means that they’re independent of each other. By ORing them you get 1 | 2 | 4 which is equal to 7 and you can detect each individual bit with other boolean operations (see here and here for an explanation of these).

    If they were defined as 1, 2, 3 and 4 then you’d probably either have to make one call for each side or you’d have to pass four different parameters, one per side. Otherwise you couldn’t tell the difference between LEFT and RIGHT (1 + 2 = 3) and TOP (3), since both of them would be the same value (with a simple addition operation).

    The 0x stuff is just hexadecimal numbers which are easier to see as binary bitmasks (each hexadecimal digit corresponds exactly with four binary digits. You’ll tend to see patterns like 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 and so on, since they’re the equivalent of a single 1 bit moving towards the most significant bit position – those values are equivalent to binary 00000001, 00000010, 00000100, 00001000, 00010000, 00100000 and so on.

    Aside: Once you get used to hex, you rarely have to worry about the 1 << n stuff. You can instantly recognise 0x4000 as binary 0100 0000 0000 0000. That’s less obvious if you see the value 16384 in the code although some of us even recognise that 🙂

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

Sidebar

Related Questions

One thing I have never truly understood is the concept of character encoding. The
One thing I really like about AS3 over AS2 is how much more compile-time
one thing that i always wondered, if i have a method like this: String
There is one thing in PHP that i personally really like to use -
One thing that really bothers me about Google Code is that fact that it
One thing I have continually found very confusing about using an object database like
One thing that I like to do from time to time is do a
One thing I really love about languages such as Python is that if you
One thing I like about JSP is include mechanism. In JSP I can simply
One thing I really, really miss from Javadoc is the ability to see which

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.