73 questions
0
votes
2
answers
185
views
C program for changing binary to signed integer doesn't work properly
I have a C program that I made that is supposed to convert binary into a signed integer, but it does not give the correct result.
Here is the function of the code that converts binary numbers to ...
0
votes
1
answer
127
views
Unsigned int weird behavior with Loops in C
In C, I encounter this weird problem:
For the following code:
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
unsigned int i = 0;
for (int j = i - 1; j < ...
-3
votes
2
answers
106
views
Why is the expected output not displayed [duplicate]
why there is no output for this?
#include <stdio.h>
int main() {
int i;
int array[4] = {10, 25, 36, 42};
// int size=sizeof(array) / sizeof(int);
for (i = -1; i < sizeof(...
0
votes
0
answers
77
views
subtraction of 2 sign and magnitude binary numbers in assembly (ex : 11110110 and 10101011)
how can I subtract 2 sign and magnitude binary numbers in assembly (ex : 11110110 and 10101011)
I used SAL then I don't know how to pick out the sign bit, when we shift to left, our signed bit is ...
3
votes
3
answers
240
views
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);
...
1
vote
0
answers
51
views
Bit shifting signed and unsigned integers
using (MemoryStream mem = new MemoryStream (bytes.Skip(4).toArray()))
{
using (BinaryReader reader = new BinaryReader(mem))
{
UInt32 time1;
UInt32 time2;
Int64 time = 0;
try{
time1 =...
0
votes
1
answer
156
views
Idiomatically convert signed to unsigned types if you know that value will be positive
Compare the following code. It's clear from the context that one pointer will always be bigger than the other and the result of std::distance() therefore positive. How do I idiomatically convert from ...
0
votes
1
answer
491
views
How to subtract negative numbers from negative numbers using Complement notation?
I've just started learning. I am trying to understand this thoroughly and deeply. I somewhat understand it as long as it's subtraction between a Smaller number from a Larger number or a Larger number ...
2
votes
1
answer
173
views
Doing calculation in the range 0f 10^30
The following code is giving an error by saying the message "singed integer overflow".
And the error is in this test case :
input : 208170109961052
Output : 4611790103482368430
Expected ...
2
votes
2
answers
451
views
Why, in some C++ compilers, does `int x = 2147483647+1;` give only a warning but stores a negative value, while some compilers give a runtime error?
I want to check if the reverse of an signed int value x lies inside INT_MAX and INT_MIN. For this, I have reversed x twice and checked if it is equal to the original x, if so then it lies inside ...
1
vote
2
answers
280
views
About int and unsigned int
There are the following programs:
#include <stdio.h>
int main(void)
{
int i=2147483647;
unsigned int j=4294967295;
printf("%d %d %d\n",i,i+1,i+2);
printf("%u %u %u\n&...
0
votes
2
answers
480
views
C usual arithmetic conversions rules
... the integer promotions are performed on both operands.
Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
...
4
votes
3
answers
543
views
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 <stdio.h>
...
0
votes
2
answers
6k
views
Converting unsigned to signed integers (Using Excel formulas or Power Query)
I have a list of unsigned 32-bit unsigned integers that actually are supposed to represent 32-bit signed integers.
For example 62714 = binary 1111010011111010 = signed -2822 when interpreted as two's ...
-3
votes
1
answer
652
views
How does sign contraction work from 16 bit to 8 bit?
How do I sign contract ff12.here it is a negative number but I need to remove all the FF here but if I do that it becomes a positive number.
One definition of "sign contraction" can be ...
1
vote
2
answers
143
views
How to access Json element with the name "long" and "short"
I am trying to access a json element with the name "long", but VS gives an error, it detects it as a 16 bit signed integer.
The other elements in Json I can access , except element "...
2
votes
2
answers
119
views
С typecasting in conditions
There is a simple example:
unsigned a = -5;
int b = 5;
if (a + b <= -1){
...
}
To which type will cast a+b? To signed or unsigned? And is it noted in C-standard or compiler will decide what to do?
0
votes
2
answers
941
views
Unsigned modulo in Java
I'm porting some c code which is doing a modulo on an uint32_t. An uint32_t fits in a Java int bit-wise, but I cannot figure out how to perform a modulo operation on it without converting to a long. ...
0
votes
0
answers
109
views
How Int class in python represent negative numbers
Do we can view, how class int in python handles negative numbers by using two's complement?
I want to see source code for that not theories, I already understand how that happens in cs but I don't ...
0
votes
1
answer
565
views
how to print negative integer using SWORD PTR(assembly lang)
I'm learning x86 assembly language and have no idea how to print negative integer using Irvine32 library.
INCLUDE Irvine32.inc
.data
arry SWORD 10, -20, 30
.code
main PROC
mov eax, ...
0
votes
1
answer
100
views
mod(x,y) works for unsigned integers, but not for signed integers in nand2tetris. What changes should I make?
@R2
M=0
@R0
D=M
@END
D, JEQ
@store
M=D
(LOOP)
@R1
D=D-M
@REMAINDER
D, JLT
@EVENLY
D, JEQ
@LOOP
0, JMP
(REMAINDER)
@R1
D=D+M
@R2
...
2
votes
2
answers
500
views
Unsigned user-defined integer literal
Suppose I want to define an integer literal which also allows for negative values, e.g. -12_km.
I.e., I would like to do
using coord_t = long long;
coord_t operator "" _km(long long int);
...
1
vote
1
answer
988
views
Unsigned Multiplication using Signed Multiplier
I have been assigned with a task to perform unsigned multiplication using signed multiplier. Despite multiple attempts, I couldn't get it. Is it possible to do this?
Code:
#include <stdio.h>
int ...
0
votes
0
answers
828
views
C# Masking a signed int with a hex mask that "looks like" UInt
I have a problem with masking an Int16, C# interprets the mask's high hex number (65532d) as an Int32.
I have seen many examples but they have either UInt or use friendly sunshine low-bit masks that ...
0
votes
2
answers
515
views
Is the two's complement of a negative integer used only for addition and subtraction?
So for example,
13 in binary is: 00000000 00000000 00000000 00001101.
Would -13 be stored as 10000000 00000000 00000000 00001101 (using the most significant bit to represent the sign) or would it be ...
3
votes
2
answers
2k
views
How to convert negative integer to string and output it in MASM Assembly
I am supposed to take signed integers from a user, calculate the sum of inputted numbers and then display the average. The problem is, negative numbers don't seem to be displaying correctly, although ...
0
votes
0
answers
350
views
How to drop '+' or '-' characters in MASM Assembly
In MASM Assembly, if the user writes "-22", I want to check the first character for a "-" sign and then drop it.
If the user writes "+12", I want to check the first character for a "+" sign and then ...
0
votes
0
answers
48
views
Java convert from signed to unsigned in a single byte [duplicate]
I have a byte b that in Java represents signed integers from -128 to +127. How do I convert it to represent only (positive) unsigned integers? I don't want to use int or long but a 1-byte ...
0
votes
0
answers
539
views
Convert HEX to Signed INT Javascript [duplicate]
I am trying to convert this HEX value fba46c58 to a signed int then take that number and divide it by 1000000. I have tried the following code but I don't seem to come up with the correct number. ...
-1
votes
1
answer
145
views
Convert normal integer to 8 bit integer in PHP [closed]
I have an Integer 138 that i want to covert into an 8 bit signed Integer that will become -118.
I have tried unpack, intval and several othe php function but they didn't worked.
How should i do this, ...
9
votes
1
answer
13k
views
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 ...
97
votes
6
answers
9k
views
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 <stdio.h>
struct mystruct { int enabled:1; };
int main()
{
struct mystruct s;
s.enabled = 1;
if(s.enabled == 1)
printf("Is enabled\n"...
0
votes
1
answer
585
views
Hashing int16_t to uint64_t [closed]
I'm trying to make a hash function for int16_t. The function prototype looks like this:
uint64_t hash_int16_t(const void *key);
So far I've gotten this but I don't know if this is the correct ...
9
votes
2
answers
6k
views
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 ...
0
votes
1
answer
1k
views
What are the specifics of STORING a 32 bit signed integer into a register?
My number type is a signed two’s complement integer.
In memory registers %rdi/edi/di, I have 0xFFFFFFFF.
In %rsi/esi/si, I have 0x80000000.
My instruction is addl %edi, %esi.
How do I properly add ...
2
votes
3
answers
4k
views
Convert two bytes to a signed integer in java
Question in short
Is there a way to read two bytes as a signed integer?
Details & example
Given two bytes in java, each represents an integer, we can convert them to the int value they ...
4
votes
2
answers
2k
views
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 ...
1
vote
4
answers
5k
views
What does actually mean by the statement "PHP does not support unsigned integers"?
I am using PHP 7.0.2
At one place in the manual on integers, I saw the below statement :
Integers can be specified in decimal (base 10), hexadecimal (base 16),
octal (base 8) or binary (base 2) ...
2
votes
2
answers
513
views
Assembly imul signed
thx for help my question is about ax value received from code below?
mov al,22h
mov cl,0fdh
imul cl
Actual machine result: ff9a
What I expected: 00:9a (by multiplying in binary)
The first number is ...
2
votes
2
answers
1k
views
Unreasonable behavior on mixing signed and unsigned integers
Motivated from a code snippet on this blog under "What happens when I mix signed and unsigned integers?" I decided to run it with few different values of signed and unsigned integers and observe the ...
0
votes
1
answer
368
views
Reading a fortran generated binary file into a signed integer array in C++
I am learning C++. I want to read some files generated by a fortran program in a C++ program. Each of the files I want to read contains a list of numbers which are either 0, 1, or -1. These numbers ...
0
votes
3
answers
2k
views
Convert binary to signed int/decimal
I am trying to convert hex data to signed int/decimal and can't figure out what I'm doing wrong.
I need FE to turn into -2.
I'm using Convert.ToInt32(fields[10], 16) but am getting 254 instead of -...
5
votes
1
answer
8k
views
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....
0
votes
1
answer
959
views
How to convert NSString with negative integer into negative integer
In my app, I want to display 0 if NSString contains negative integers. I tried to convert NSString into signed int but my NSString to signed int conversion code always returns 0 value.
Eg: I want ...
2
votes
3
answers
4k
views
Which string hashing algorithm produces 32-bit or 64-bit signed integers?
I want to hash strings of variable length (6-60 characters long) to 32-bit signed integers in order to save disk space in PostgreSQL.
I don't want to encrypt any data, and the hashing function needs ...
0
votes
4
answers
408
views
Largest positive value of a signed integer type of unknown size
If we have a unsigned integer type which we may not know the size of, for example size_t, then we can get the largest value it can hold relatively simply with something like:
size_t maximal = -1;
Is ...
-2
votes
2
answers
579
views
Why is int i = 1<<31 == -2147483648 instead of 2147483648? [duplicate]
I was trying to understand the bitwise operation and according to me integer contains 32 bit and from LSB 0th position to MSB 31st position so if I set left shift 1 to 31 place I think I should get 2^...
1
vote
0
answers
985
views
Signed Memory Arithmetic vulnerability iOS
I developed a Cordova application and it went through a security review, one of the findings was related to a plugin I use to make curl requests. The finding is graded as Heigh vulnerability.
The ...
1
vote
2
answers
1k
views
How can I divide a signed integer using only binary operators?
I can only use ! ~ & ^ | + << >>
I am writing this in C.
I am trying to divide a number x by 2^n.
So i thought if I shift x >> n that would work, but it does not work with odd negative ...
3
votes
1
answer
260
views
Signed int in c language assignd bitfields but confusion
Currently I am working on c and facing a confusion regarding signed int in structure and here I have given the example:
#include <stdio.h>
#include <string.h>
struct {
signed int age : ...