1

I need to calculate signed value range for some bits count.
For example:

bitscount = 4

min value = -2^3;

max value = 2^3 - 1; in bitwise it is (1<<3 - 1)

I forgot how to compose bitwise for min value. Please help.

1
  • 4
    Well how about -(1 << 3)? You already know that 2^3 is 1<<3, so just negate it.. Commented Nov 18, 2014 at 7:20

2 Answers 2

1
minValue = -(1 << (bitscount - 1));
maxValue = (1 << (bitscount - 1)) - 1;
Sign up to request clarification or add additional context in comments.

1 Comment

this answer has been flagged as low quality answer, please provide some explanation.
1

Try like this:

int bits_count = 4;  // between 2 ~ 32 (I assumed int type is 32bits signed integer)
int min_value = -(1 << (bits_count - 1));
int max_value = (1 << (bits_count - 1)) - 1;

And reference site: http://www.tutorialspoint.com/java/java_bitwise_operators_examples.htm

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.