How to get the last n digits of a hexadecimal number? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I need a really short way to extract the last 4 digits of a hex number. So an input of 0x2479c should throw an output of 0x479c. I want to avoid converting and reconverting to binary.
Modulo division, which would generally work for decimal numbers, does not work in this case.
0x2479c modulo 0xffff = 0x479e
which isn't correct. I'm trying to achieve this is c/c++.

You should use a mask and then a byte wise 'and' with an other value. In your case :
0x2479c & 0x0ffff

Either use a mask
0x2479c & 0x0ffff
or the modulo operator
0x2479c % (0x10000);
You were off by one in the operand of modulo.

Related

counting odd and even digts in a given number using C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can I count the even and the odd integers in a given number in C++?
For example: The user inputs: 32478
output: 3 even numbers and 2 odd numbers.
The basic algorithm is:
Take the number modulo 2 (num % 2). If the result is 1 then the number is odd; increment the odd counter. If not then it's even; increment the even counter.
Divide the number by 10, dropping the remainder. (num /= 10)
Go back to step 1 if the number isn't zero.

How to design an algorithm that multiplies two floats without '*'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How do I design an algorithm that takes two floats and multiplies them using only addition, bit shifting and bitwise operations?
I have already found one like this for integers, but that doesn't work with floats.
I have also found another that is much like what I need but log is also prohibited in my case.
The floats are stored according to the IEEE754 standard. I have also tried to keep their exponent part, and bitwise multiply their fractional part with no luck.
According to http://en.wikipedia.org/wiki/IEEE_floating_point, an IEEE754 number x = (-1)^s * c * b^q is represented by s,c,b,q , all are integers. for Two floating point numbers with the same base b is the same.
So the multiplication of two floating point numbers x and y is:
(-1)^(s1+s2)*c1*c2*b^(q1+q2) so the new floating point is represented by: s1+s2, c1*c2, b q1+q2 so you only have left to deal with multiplication of c1 and c2, both are integers so you are done.

Regex Binary multiple of 4 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I write a Reg-ex Expression to check whether a string is a binary multiple of 4? I am not good at making DFA and finding expressions.
A multiple of 4 in binary is any binary number that ends with 00, so this regexp should do it:
^(?:[10]*00|00?)$
If you mean a multiple of 4 in decimal, I wouldn't do that with a regexp, except perhaps to verify that it's a number. Then I'd parse it and check whether number % 4 is zero.

regex to require a positive numeric value (5,2) forbidding leading zero [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can a regex pattern enforce the following combination of constraints on a numeric value?
the number must be >= 1 and <= 999
(decimal point cannot be the first character in the string?)
the number can be an integer or a number with a fractional component
when it has a fractional component,
no more than 2 digits to the right of the decimal point
EDIT: but at least one digit to the right
must not have leading zero(s)
Perl syntax:
^+?(?:(?:999(?:\.0{1,2})?)|(?:(?!999)[1-9]\d{0,2}(?:\.\d{1,2})?))$
I'll go with this one:
/^
(?:
999.0* # 999.000
| # or
[1-9]\d{,2}((?<!999)\.\d+)? # 1-998 plus optional .\d*
)
$/x
Yes..
^(?!999[.](0*[1-9]+$))(?!0|[.])\d{1,3}([.]\d{1,2})?$

Regex pattern for numeric values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need an regular expression pattern to only accept positive whole numbers. It can also accept a single zero.
I do not want to accept decimals, negative number and numbers with leading zeros.
Any suggestions?
^(0|[1-9][0-9]*)$
"[1-9][0-9]*|0"
I'd just use "[0-9]+" to represent positive whole numbers.
This will allow decimal numbers (or whole numbers) that don't start with zero:
^(([1-9]*)|(([1-9]*)\.([0-9]*)))$
If you want to allow numbers that start with zero, you can do :
^(([0-9]*)|(([0-9]*)\.([0-9]*)))$
/([1-9][0-9]*)|0/
/^0|[1-9]\d*$/