Questions tagged [signed-integer]
71 questions
146
votes
5 answers
What is the difference between signed and unsigned int
What is the difference between signed and unsigned int?

Moumita Das
- 1,485
- 2
- 12
- 5
98
votes
6 answers
Why is assigning a value to a bit field not giving the same value back?
I saw the below code in this Quora post:
#include
struct mystruct { int enabled:1; };
int main()
{
struct mystruct s;
s.enabled = 1;
if(s.enabled == 1)
printf("Is enabled\n"); // --> we think this to be printed
else
…

iammilind
- 68,093
- 33
- 169
- 336
57
votes
5 answers
What is the use of intptr_t?
I know it is an integer type that can be cast to/from pointer without loss of data, but why would I ever want to do this? What advantage does having an integer type have over void* for holding the pointer and THE_REAL_TYPE* for pointer…

Baruch
- 20,590
- 28
- 126
- 201
42
votes
5 answers
What is a difference between unsigned int and signed int in C?
Consider these definitions:
int x=5;
int y=-5;
unsigned int z=5;
How are they stored in memory? Can anybody explain the bit representation of these in memory?
Can int x=5 and int y=-5 have same bit representation in memory?

Anand Kumar
- 499
- 1
- 4
- 7
22
votes
4 answers
What is going on with bitwise operators and integer promotion?
I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size.
#include
#include
#include
int main()
{
uint8_t x = 12;
std::cout << (x << 1) << '\n';
std::cout << ~x;
…

Wandering Fool
- 2,170
- 3
- 18
- 48
22
votes
2 answers
Is comparing an underflowed, unsigned integer to -1 well-defined?
Consider the following†:
size_t r = 0;
r--;
const bool result = (r == -1);
Does the comparison whose result initialises result have well-defined behaviour?
And is its result true, as I'd expect?
This Q&A was written because I was unsure of two…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
17
votes
5 answers
Fast sign of integer in C
There is a sign function in C:
int sign(int x)
{
if(x > 0) return 1;
if(x < 0) return -1;
return 0;
}
Unfortunately, comparison cost is very high, so I need to modify function in order reduce the number of comparisons.
I tried the…

Alex
- 9,891
- 11
- 53
- 87
8
votes
1 answer
What is signed division(idiv) instruction?
In intel instruction, idiv(integer divsion) means signed division.
I got the result of idiv, but I don't quite understand the result.
- Example
0xffff0000 idiv 0xffff1100
- My wrong prediction
As long as I know, quotient should be 0, and…

Jiwon
- 1,074
- 1
- 11
- 27
8
votes
2 answers
How to implement arithmetic right shift in C
Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2b ⌋, where a, b are signed (a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following…

DaBler
- 2,695
- 2
- 26
- 46
5
votes
1 answer
Displaying numbers with DOS
I was tasked to write a program that displays the linear address of my
program's PSP. I wrote the following:
ORG 256
mov dx,Msg
mov ah,09h ;DOS.WriteStringToStandardOutput
int 21h
…

Sep Roland
- 33,889
- 7
- 43
- 76
4
votes
3 answers
Why do `(char)~0` and `(unsigned char)~0` return values of different widths?
I bumped into this while writing a program trying to print the constituent byte values of UTF-8 characters.
This is the program that I wrote to test the various ~0 operations:
#include
int main()
{
printf("%x\n", (char)~0); //…

Marcus Harrison
- 819
- 6
- 19
4
votes
2 answers
C - three bytes into one signed int
I have a sensor which gives its output in three bytes. I read it like this:
unsigned char byte0,byte1,byte2;
byte0=readRegister(0x25);
byte1=readRegister(0x26);
byte2=readRegister(0x27);
Now I want these three bytes merged into one number:
int…

muliku
- 416
- 3
- 17
4
votes
1 answer
24- to 32-bit conversion in Java
I am interested in a new IoT project called OpenBCI, which is basically an open source EEG for reading and processing brain waves and other biodata. In their docs they claim that the data transmitted over-the-air (via RFDuino) sends 24-bit data. To…

smeeb
- 27,777
- 57
- 250
- 447
4
votes
1 answer
SDL_Keycodes are too big for storage
While searching up methods of detecting multiple keys at once in SDL 2, I came across this piece of code for SDL 1.x:
//author: Rob Loach
// Global key buffer
bool keys[256];
while(SDL_PollEvent(&mainEvent))
{
if(mainEvent.type == SDL_KEYDOWN)
…

hpm
- 1,202
- 13
- 34
3
votes
3 answers
Questions about C strlen function
I tried to compare with strlen(string) with -1 but different methods gave different results:
char string[] = {"1234"};
int len = strlen(string);
int bool;
bool = -1 < strlen(string);
printf("%d",bool); //bool=0
bool = -1 < len;
printf("%d",bool);…

chaganhu
- 33
- 3