Does C++ memset only works for 0 and -1? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1. Why not 3 3

Does C++ memset only works for 0 and -1?
It does work for all byte values.
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1.
I doubt that. Either your system is broken, or you made a mistake.
Why not 3 3
Because that's not what std::memset does. It sets every byte to the value that you provide. A multi-byte integer whose each byte have the value 3 doesn't have the value 3. In the value 3, only the least significant byte would have the value 3 and the more significant bytes would be 0.
Unless you want a repeating byte pattern, std::memset won't be useful to you. If you simply want to assign a value to each element, then you should be using std::fill or its friends:
std::fill(std::begin(a), std::end(a), 3);
Or just a bare loop:
for(int& i : a)
i = 3;
0 happens to be the only byte pattern that preserves the value across all byte widths on all systems; -1 is another but only on 2's complement systems. On 1's complement systems the other pattern has the value -0. This is why std::memset incidentally behaves the same as std::fill when using those values and only when using those values.

Related

Convert a number from base A to base B [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Given two positive integers A and B and a string S of size N, denoting a number in base A, the task is to convert the given string S from base A to base B.
Example:
"25"
Base A : 6
Base B : 8
Expected Result : 21
Explanation:
The Number 25 is represented in base 6 format. The goal is to convert it to base 8 which in this case is 21.
divide the number by base.
write down the remainder.
repeat the process with the integer part of the division.
stop when you reach zero.
the remainders in reverse order give you the digits in base
With an array with the chars that corresponds to the base in order, base 32 should be like:
char digits[32] = "0123456789abcdefghijklmnopqrstuv";
I would use recursivity, but its up to you. When you divide, use the remainder in the array, so if the remainder is 12 for example, you will have the 'c' char.
If you have a string instead of a number for the original number, you can use string[i]-'a'+10 in the letters to obtain the corresponding number.
use itoa
char * itoa ( int value, char * str, int base );
https://www.cplusplus.com/reference/cstdlib/itoa/

The resulting type when a float variable is divided by an integer variable in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was solving a question on geeksforgeeks.com on c++ where I determining the resulting type of the variable after operations.
Input:
1
1 2 3 5
gfgc
Output:
4 8 4 8
32 1
Example:
Testcase 1:
b/c = 2/3 =>sizeof(2/3)=>float size is 4 bytes
"b/a = 2/1 =>sizeof(2/1)=>double size is 8 bytes"
c/a = 3/1 =>sizeof(3/1)=>integer size is 4 bytes
(c/a)+l =3+5= 8 =>sizeof(8)=>long long size is 8 bytes
sizeof(gfgc) = 32 => It is not 4 because of the reason listed here
sizeof(c) = 1 as it is just a character.
Can someone explain the reason behind the output shown in the line in quotes?
Given float a and int b (or vice versa), and the expression auto c = a/b, both a and b are coerced into floats for the division, and c is a float.
More generally, if either operand is of a floating-point type, the resulting type will the first in this list that's found in the expression: long double, double, or float; the other operand is converted to that type. If both operands are integers then a similar sort of rule applies.
These are the "usual arithmetic conversions". Refer to [expr.arith.conv].

Determine offset using only bitmask [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello' I've got a problem
there is some telemetry values packed in words (or double words) and bitmask for each telemetry channel.
for example i've got mask 0xf000 and word value 0x2499
after & operation i've got 0x2000,but real telemetry value packed in this word is 0x2 (0x2000>>12). How can i determine this offset using only mask and without cycles like:
offset = 0;
for (int i = 0;i<32 i++)
{
if ((mask>>i)&1)
{
offset = i
break;
}
}
I think you may use some built in functions to get the first non-zero bit index.For example GCC has int __builtin_ffs (int x)
— Built-in Function: int __builtin_ffs (int x)
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
So in your case:
offset = __builtin_ffs(mask);
MSDN has similar intrinsics _BitScanForward and _BitScanForward64
https://msdn.microsoft.com/en-us/library/wfd9z0bb.aspx
All these builtin functions use special assembly instruction which performs the required calculation on the hardware level.
Maybe you can look at intrinsics to perform a "scan". The instruction "int _bit_scan_forward(int)" should do the trick ;)
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#cats=Bit%20Manipulation

Create an overflow at a given value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If a unsigned byte overflows it goes from 255 to 0 and vica versa -1 gives 255.
Would it be possible to have it overflow at for example 200?
Without using if statements.
Overflow is fairly simple:
unsigned int a = 150, b = 150;
a += b; // do an operation
a %= 200; // wrap it
However, with underflow, it's a bit harder (see orlp's answer for this).
To make it less error prone if you use this variable several times, in C++ with operator overloading, you can make a class that simulates an integer type which wraps after every operation with operator overloading.
The modulo operator does what you want, with some trickery for negative values:
int wrap(int x, int n) {
return x < 0 ? ((x % n) + n) % n : x % n;
}
// wrap(205, 200) == 5
// wrap(-1, 200) == 199
Unless your willing to learn assembly, such an action would be impossible for several reasons.
All the types like char, short, int, etc. are builtin and predefined by the parser.
200 isnt a power of two; computer represent numbers in binary.
Note: The above is only true if you want implicit overflow; modulas lets you do explicit overflow.

Advanced pointer typecasting in C/C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've searched all the net, but I didn't find any thing.
I want to know how it makes sense for example when you type :
char *buffer = "Everything! such as shellcodes"
int ret;
ret = (int *)& ret +2 ;
(*ret) = (int)buffer;
Or when you use :
(* (void(*) ()) shellcode)();
Which shellcode is char pointer.
I know & gives the address of a variable but I don't know the
(int *) then address of something
I think it overwrites the EIP.
but the second code what does it actually do ?
I know maybe it not be good question, but I want to know how to get deep concept about pointers and their typecasting.
Would you mind giving me the concepts of the matter by an article or book or ... .
I want to learn this to understand and write such a thing by oneself.
Thank you so much.
The memory simply contains bytes of data. It is up to you how you interpret this data.
Your buffer may be interpreted as int. For example:
char buffer[] = {1, 2, 3, 4};
int number = *(int *)buffer;
Running the code on an Intel x86 processor would result in number having the value of 0x04030201, as integers are stored as 32bit (4 bytes) little-endian buffers.
Your buffer can also be interpreted as code. The C code return 5; may be compiled into the following x86 assembly code:
mov eax, 5
retn
This code looks in the memory as B8 05 00 00 00 C3
So let's look on the following example:
char buffer[] = {0xb8, 5, 0, 0, 0, 0xc3};
int number = ((int (*)())(buffer))();
Running this code on an Intel x86 processor (assuming you compile this code without security checks that prevent the execution of data buffers) would result in number having the value of 5; The code casts the pointer to buffer into a pointer to a function with the signature int func_name() and then executes it.