Solved

negative value

Posted on 2011-02-17
5
427 Views
Last Modified: 2012-05-11


static int reg, data_varde_hex[10000];
static char data_reg_hex[10000][5]

data_varde_hex[reg] = strtol(data_reg_hex[reg], 0, 16);            

//So far it works fine, but if data_reg_hex are FFEC a expect a negative value,
//how can i do to get a negative value.
//
// Thanks
//
// Ludde72

0
Comment
Question by:Ludde72
5 Comments
 
LVL 3

Expert Comment

by:imaki06
Comment Utility
Try short instead of the int, for the data_varde_hex
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
This gives -20
static int data_varde_hex;
   static short shortVal;
   static char data_reg_hex[5] = "FFEC";

   data_varde_hex = strtol(data_reg_hex, 0, 16);  
   shortVal = (short)strtol(data_reg_hex, 0, 16);

Open in new window

0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi ludde,

The value 0xFFEC is not a negative value.  It is the bit stream 1111111111101100.

That value becomes negative when the hardware recognizes that the leftmost (high-order) bit is a 1.

Because the value is exactly 16 bits, placing it in a signed, 16-bit integer variable is sufficient for C to treat it as negative.  Placing it in a signed, 8-bit (char) variable will also work because the top 9 bits are 1.

But putting it in a 32-bit or 64-bit will not because C doesn't automatically sign extend.  The top be will not be carried to the left through the sign bit.  And C makes no rules on the length of short, int, long, and long long variables (except with relation to each other).

Most compilers today treat an *int* as 32 bits.  To ensure that a negative value is stored, you'll need to force a sign extended value.  And because you know that the value is 16 bits it is easy to do.

  int Value.
  unsigned int X = 0xFFEC;    // Can be any value, including the sample 16-bit value

  if (X >> 15)    // Check for the top bit
  {
    Value = -1;                       // Sets all bits in the variable;
    Value ^= (X ^ 0xFFFF);    // Merge in the value of X, sign extended
  }
  else
    Value = X;


There are certainly other ways to sign extend an integer, but this is pretty straight forward.


Good Luck,
Kent
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Here is another way to get -20 into an int.
static int data_varde_hex;
   static char data_reg_hex[5] = "FFEC";
   data_varde_hex = (short)strtol(data_reg_hex, 0, 16);  // now this give -20 also

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
(X ^ 0x8000) - 0x8000
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now