Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# negative value

Posted on 2011-02-17
Medium Priority
474 Views

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
Question by:Ludde72
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Expert Comment

ID: 34915359
Try short instead of the int, for the data_varde_hex
0

LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 34915376
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);
``````
0

LVL 46

Expert Comment

ID: 34916002
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

ID: 34916083
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
``````
0

LVL 84

Expert Comment

ID: 34916245
(X ^ 0x8000) - 0x8000
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll