I'm trying to make program that will for for example 273(000100010001b) make to 4095(111111111111b).
Funny part is that my program works for first 2 iterations, for example 17(00010001b) correctly returns 255(11111111b) but every byte after that doesn't work. For instance 273(000100010001b) returns 61951(1111000111111111b) and I cant figure out why is that.
Here is my code
int x,temp, i;
int mask = 15;
scanf_s("%d", &x);
temp = x;
for(i=0;i<4;i++){
mask<<=i*4;
if((temp&1)==1){
x|=mask;
} else{
}
temp>>=4;
}
printf("%d", x);
The issue is the shift you perform on mask. You shift it by 0 on the 1st iteration, 4 on the 2nd, 12 (4 + 8) on the 3rd and so on.
Also, you don't apply the mask over all the bits of temp. I can't tell if you do this on purpose.
Here you are a fixed version:
int foo(int x) {
/* ^ I didn't know how to call such function */
static size_t const mask_size = 4;
unsigned mask = 15;
int temp = x;
for (unsigned i = 0; i < (CHAR_BIT * sizeof(int)) / mask_size; ++i) {
if ((temp & 1) == 1) {
x |= mask;
}
mask <<= mask_size;
temp >>= mask_size;
}
return x;
}
My results are:
in: 17(10) = 10001(2)
out: 255(10) = 11111111(2)
in: 273(10) = 100010001(2)
out: 4095(10) = 111111111111(2)
in: 4369(10) = 1000100010001(2)
out: 65535(10) = 1111111111111111(2)
I need help converting a number string to a SQL_NUMERIC_STRUCT value to use decimal and numeric database data types. The SQL_NUMERIC_STRUCT value is a 16-byte hexadecimal unsigned integer. For example, I have a string "12312312899012312890522341231231232198", that contains 38 digits (maximum for SQL SERVER decimal or numeric data types). In other languages such a c# there is a built-in conversion function, but my Visual studio 2019 does not allow me to directly use 128-bit integers in the C++ environment. The Microsoft help page offers example with a small,2-byte integer, unfortunately.
I have found a solution.
bool ConvertToNumericStruct (char* s, SQL_NUMERIC_STRUCT* v){
int sc = (int)strlen(s), scale = 0, i,y, z;
char c, p = 0, d; bool minus = false;
int _tmp, x, carryover;
memset(v->val, 0, 16);
for (i = 0; i < sc; i++) {
c = s[i];
if (i == 0 && c == '-')minus = true;
else if (c == '.') { if (scale == 0)scale = sc - i - 1; else return false; }
else if (c < '0' || c>'9') return false;
else
{
if (p > 38) return false;
d = c - 48;
_tmp = 0;
carryover = d;
y = 0; z = 0;
for (x = sc - 1; x > -1; x--)
{
if (y % 2 == 1)
{
_tmp = (v->val[z] >> 4) * 10 + carryover;
v->val[z] &= 0x0F;
v->val[z] |= ((_tmp % 16) << 4 & 0xF0);
z++;
if (z > 15) break;
}
else {
_tmp = (v->val[z] & 0x0F) * 10 + carryover;
v->val[z] &= 0Xf0;
v->val[z] |= ((_tmp % 16) & 0x0F);
}
y++;
carryover = _tmp / 16;
}
p++;
}
}
v->precision = p;
v->scale = scale;
if (minus) v->sign = 0; else v->sign = 1;
return true;}
If you want to insert data defined by decimal or numeric into database such as MySql via UnixODBC with the function SQLBindParameter,you can just use SQL_C_CHAR for fCtype and SQL_CHAR for fSqltype with a char-string buffer.No need to convert.That would be done implicitly.
Here is a sample code
int x = rand() % 90000;
When I was doing something like this I realized all the numbers were around 0 - 30000.
Is there a limit for this? If there is how can I use it without limits?
You should not use rand(), because it is quite poor in many C standard library implementations. It will return a pseudorandom number between 0 and RAND_MAX, inclusive, but RAND_MAX is often relatively small; for example, 32767.
Using the modulo operator to yield a range of integers is problematic if the range is a large fraction of the range of values the generator function can return, because the distribution is not exactly uniform.
For example, let's say RAND_MAX is 59999, and we used rand() % 40000. The probability of the result being between 0 and 19999 is 67%, but only 33% between 20000 and 39999. This is because that rand() produces a value in [0,19999] at 1/3 probability, [20000,39999] at 1/3 probability, and [40000..59999] at 1/3 probability; but that last third folds back so that it yields [0,19999] after the modulo operation!
For small ranges the bias is not so noticeable.
Personally, I like to generate enough random bits to cover the desired range, then use the exclusion method to pick the value.
If we need to use rand(), we can use the following helper function to generate a pseudorandom number whose range is at least atleast (but may be larger; i.e. it can return a larger value):
#include <inttypes.h>
static inline uint64_t rand_atleast(uint64_t atleast)
{
uint64_t result = 0;
do {
result = ((uint64_t)RAND_MAX + 1) * result + (uint64_t)rand();
atleast /= ((uint64_t)RAND_MAX + 1);
} while (atleast > 0);
return result;
}
To use the exclusion method to create ints within a desired range, we can use a structure to contain the stuff we need, a helper function to initialize that range (to describe some specific range of ints), and another helper function to generate integers within that range:
struct range_spec {
uint64_t mask;
uint64_t limit;
int base;
};
static inline void set_range(struct range_spec *spec,
int minimum, int maximum)
{
uint64_t mask;
int base;
if (minimum <= maximum) {
base = minimum;
mask = maximum - minimum;
} else {
base = maximum;
mask = minimum - maximum;
}
spec->base = base;
spec->limit = mask;
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
mask |= mask >> 32;
spec->mask = mask;
}
static inline int rand_range(const struct range_spec *spec)
{
const uint64_t mask = spec->mask;
const uint64_t limit = spec->limit;
uint64_t result;
do {
result = rand_atleast(mask) & mask;
} while (result > limit);
return spec->base + result;
}
However, this is a lot of work to get pretty poor pseudorandom numbers: not worth it in my opinion.
I usually use Xorshift64* instead. It is fast, quite random (see this extended comment of mine), and really easy to implement.
Essentially, you can use a small header file, say rng64.h:
#ifndef RNG64_H
#define RNG64_H
#include <inttypes.h>
#include <time.h>
typedef struct {
uint64_t limit;
int64_t base;
int shift;
} rng64_intrange_spec;
static uint64_t rng64_state = 1;
static inline uint64_t rng64(void)
{
uint64_t x = rng64_state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
rng64_state = x;
return x * UINT64_C(2685821657736338717);
}
static inline uint64_t rng64_randomize(void)
{
uint64_t x;
int n = 1000;
x = ((uint64_t)time(NULL) * UINT64_C(19076794157513))
^ ((uint64_t)clock() * UINT64_C(809712647));
if (!x)
x = 1;
while (n-->0) {
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
}
rng64_state = x;
return x;
}
static inline double rng64_one(void)
{
return (double)rng64() / 18446744073709551616.0;
}
static inline int64_t rng64_intrange(rng64_intrange_spec *spec)
{
const uint64_t limit = spec->limit;
const int shift = spec->shift;
uint64_t value;
do {
value = rng64() >> shift;
} while (value > limit);
return spec->base + value;
}
static inline void rng64_set_intrange(rng64_intrange_spec *spec,
int64_t minimum,
int64_t maximum)
{
int64_t base;
uint64_t limit;
int bits = 0;
if (minimum <= maximum) {
base = minimum;
limit = maximum - minimum;
} else {
base = maximum;
limit = minimum - maximum;
}
spec->base = base;
spec->limit = limit;
while (limit >= 32768) {
limit >>= 16;
bits += 16;
}
while (limit >= 8) {
limit >>= 4;
bits += 4;
}
while (limit > 0) {
limit >>= 1;
bits += 1;
}
spec->shift = 64 - bits;
}
#endif /* RNG64_H */
Somewhere near the beginning of your program, call rng64_randomize() to generate a state based on the current time (wall clock via time(), and CPU time used to execute the current process via clock()). The initial state is churned a bit, to ensure you don't get similar sequences when running the code in quick succession. You can set the rng64_state to any value except zero, to generate a specific sequence. (Zero state will generate only zeroes.) I recommend using
printf("Using %" PRIu64 " as the Xorshift64* random number seed.\n", rng64_randomize());
which prints both the seed, and the pseudorandom number generator algorithm used, near the beginning of the program. That allows someone to reproduce the test (by setting rng64_state to that value instead of calling rng64_randomize(), or reimplement the test using their own equivalent code). Reproducibility is good.
While (uint64_t)time(NULL) is not guaranteed to work by the C standard, it does work in all current widely-used C implementations I am aware of.
If you want to compare to a different pseudorandom number generator, just reimplement another using a similar header file, and include that instead. That way you don't need to change any code that uses the generator, only the generator code itself.
rng_one() returns uniform pseudorandom numbers between 0 and 1.0, inclusive. If you want the upper limit to be exclusive, use e.g.
static inline double rng64_one(void)
{
double r;
do {
r = (double)rng64() / 18446744073709551616.0;
} while (r >= 1.0);
return r;
}
and if both limits exclusive (so it never returns 0.0 or 1.0 exactly), while (r <= 0.0 || r >= 1.0); instead.
Here's an example of how to use the rng64.h above:
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include "rng64.h"
int main(int argc, char *argv[])
{
rng64_intrange_spec r;
int minval, maxval, count, i;
char dummy;
if (argc != 4 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s MIN MAX COUNT\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "This program outputs COUNT pseudorandom integers,\n");
fprintf(stderr, "between MIN and MAX, inclusive.\n");
fprintf(stderr, "\n");
return EXIT_FAILURE;
}
if (sscanf(argv[1], " %d %c", &minval, &dummy) != 1) {
fprintf(stderr, "%s: Invalid minimum.\n", argv[1]);
return EXIT_FAILURE;
}
if (sscanf(argv[2], " %d %c", &maxval, &dummy) != 1 || maxval < minval) {
fprintf(stderr, "%s: Invalid maximum.\n", argv[2]);
return EXIT_FAILURE;
}
if (sscanf(argv[3], " %d %c", &count, &dummy) != 1 || count < 0) {
fprintf(stderr, "%s: Invalid count.\n", argv[3]);
return EXIT_FAILURE;
}
fprintf(stderr, "Generating %d pseudorandom integers in [%d, %d],\n", count, minval, maxval);
fprintf(stderr, "using Xorshift64* with seed %" PRIu64 ".\n", rng64_randomize());
fflush(stderr);
rng64_set_intrange(&r, minval, maxval);
for (i = 0; i < count; i++)
printf("%d\n", (int)rng64_intrange(&r));
return EXIT_SUCCESS;
}
Specify the minimum and maximum values (integers), and the number of integers to output, as command-line parameters.
I want to XOR two numbers as follows:
11001110 and 110
However, I need to align the bit patterns as such:
11001110
11000000
Any ideas how to do this? I imagine some bitwise operation might be needed, although how would I know how many bits to shift by?
Here's one attempt, assuming I got the requirements right:
int topbit(unsigned int x)
{
for (int i = CHAR_BIT * sizeof x - 1; i >= 0; --i)
{
if (x & (1u << i))
return i;
}
return -1;
}
unsigned int alignedxor(unsigned int a, unsigned int b)
{
const int topa = topbit(a);
const int topb = topbit(b);
if (topa < 0)
return b;
if (topb < 0)
return a;
if (topa > topb)
return a ^ (b << (topa - topb));
return (a << (topb - topa)) ^ b;
}
int main(void) {
printf("%x\n", alignedxor(0xce, 6));
printf("%x\n", alignedxor(6, 0xce));
return 0;
}
This prints e, twice, which seems correct but that's all the testing I did.
And yes, you can get the index of the topmost 1-bit more efficiently, but who cares? Also used my rich imagination to deal with corner cases (such as one number being 0).
To know how many bits to shift on Windows you can use this MS-specific function: _BitScanReverse or you can implement your own, something along the lines of:
int findFirstSetBit(uint32_t _n)
{
int idx = 31;
for( ; idx >= 0; --idx){
if(_n & (1 << idx) != 0){
return idx;
}
}
return -1;
}
I was compling my code in Arduino and suddenly I got this error:
core.a(main.cpp.o): In function `main':
D:\Personal\Arduino\arduino-1.0.4-windows\arduino- 1.0.4\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to `setup'
D:\Personal\Arduino\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'
I have no idea what that means. Here's my code:
#ifndef dht_h
#define dht_h
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#define DHT_LIB_VERSION "0.1.05"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
#define DHTLIB_INVALID_VALUE -999
#include <dht.h>
#define TIMEOUT 10000
class dht
{
public:
int read22(uint8_t pin);
double humidity;
double temperature;
private:
uint8_t bits[5]; // Buffer to receive data
int read(uint8_t pin);
};
#endif
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read22(uint8_t pin)
{
// READ VALUES
int rv = read(pin);
if (rv != DHTLIB_OK)
{
humidity = DHTLIB_INVALID_VALUE; // Invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // Invalid value
return rv;
}
// CONVERT AND STORE
humidity = word(bits[0], bits[1]) * 0.1;
if (bits[2] & 0x80) // negative temperature
{
temperature = word(bits[2]&0x7F, bits[3]) * 0.1;
temperature *= -1.0;
}
else
{
temperature = word(bits[2], bits[3]) * 0.1;
}
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
}
//Private
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_TIMEOUT
int dht::read(uint8_t pin)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t cnt = 7;
uint8_t idx = 0;
// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(20);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);
// GET ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (int i=0; i<40; i++)
{
loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0)
return DHTLIB_ERROR_TIMEOUT;
unsigned long t = micros();
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0)
return DHTLIB_ERROR_TIMEOUT;
if ((micros() - t) > 40)
bits[idx] |= (1 << cnt);
if (cnt == 0) // Next byte?
{
cnt = 7;
idx++;
}
else
cnt--;
}
return DHTLIB_OK;
}
What do I need to do to fix this code?
Every Arduino Programm needs the functions setup() and loop(), you have neither of them.
You should probably check this out.
You should add them to your main file(usually the first you created in the IDE).