Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

checking for negative!!!

Status
Not open for further replies.

t.man

New Member
hey! guys need some help.

simple things sometimes trouble a lot!!!!

how do i check for negative using bitwise AND (&)
for example, if i have 0 - 255 range and i set ref = 64, new value can be any value in range! so if negative offset occurs, invert it, like this:
Code:
offset = (new_Value -  ref);
if(offset & ?)
 {
     offset = ~offset;//2'nd complement the offset to get a positive value
 }

what should i replace '?' with, infact how will the expression (offset & ?) work?
 
I would do this

Code:
offset = new_Value - ref;

if (ref > new_value)
   offset = ref - new_Value;

If you need to do it your way

All negative 8 bit 2's comp numbers have bit 7 set to 1.
So the value to and with is 0x80.

But to form the 2'c comp you need to form the 1's comp and then add 1.
The ~ operator get you the 1's comp.

0001 // a 1
1110 // 1's comp of 1
1111 // 2's comp of 1, aka -1

1111 // -1
0000 // 1's comp of -1
0001 // 2'c comp of -1
 
3v0 said:
I would do this

Code:
offset = new_Value - ref;

if (ref > new_value)
   offset = ref - new_Value;

If you need to do it your way

All negative 8 bit 2's comp numbers have bit 7 set to 1.
So the value to and with is 0x80.

But to form the 2'c comp you need to form the 1's comp and then add 1.
The ~ operator get you the 1's comp.

0001 // a 1
1110 // 1's comp of 1
1111 // 2's comp of 1, aka -1

1111 // -1
0000 // 1's comp of -1
0001 // 2'c comp of -1

i think i get what you are saying, i was amazed with my code:
Code:
void _regulate(int value)
{
	char OFFSET;

	OFFSET = (value - SET_POINT);

	if(OFFSET & 0x80)// if negative value
	 {
		OFFSET = ~OFFSET;// 2'nd complement the OFFSET to get a positive value

		if(OFFSET > 1) //if value below 64
		{
			PORTD = GREEN; //flash yellow led
		}
		 
     }
	else if(OFFSET > 0x40)// value greater than 128
	 {
	  	PORTD = RED;//flash red led
	 }
	else//value between 64 and 128(inclusively)
	 {
		PORTD = YELLOW;//flash yellow led
	 }

my intention was to flash: GREEN LED in the range 0 - 64
YELLOW LED in the range 64 - 128
RED LED in the range 129 - 255

but instead it flashes: GREEN LED in the range 0 - 64
YELLOW LED in the range 64 - 128
RED LED in the range 129 - 191
GREEN LED in the range 192 - 255

that means i can't do it this way?or is there something i'm missing to accomplish my intention?
 
Last edited:
You are using offset as if it were both a signed and an unsigned char. You can not have it both ways.

As a signed char it can represent -128 to 127.
As an unsigned char it can represent 0 to 255.

Since you are only interested in the magnatude of the offset make it an unsigned char. Calculate the offset using the 3 lines of code from my first post. It will give you a postive value between 0 and 255.

Use simple logic to turn on the correct led
Code:
If (offset < 65)
   turn on green
else if (offset < 129)
   turn on yellow led
else
  turn on red led

You should end up with shorter code that is easier to understand.


With you current code you are getting green for the range 192 - 255.
Prior to testing you complment the number which converts it to
the range 63 to 0. Then you test to see if that number is greater then 1. Test is true and you get green.
 
Status
Not open for further replies.

Latest threads

Back
Top