int x = 2;
x = rotateInt('L', x, 1); // should return 4
x = rotateInt('R', x, 3); // should return 64
Here is the code, can someone check it and let me know what the error is?
Compilation is successful, but it says Segmentation Fault when I execute it.
int rotateInt(char direction, unsigned int x, int y)
{
int i;
for(i = 0; i < y; i++)
{
if(direction == 'R')
{
if((x & 1) == 1)
{
x = x >> 1;
x = (x ^ 128);
}
else
x = x >> 1;
}
else if(direction == 'L')
{
if((x & 128) == 1)
{
x = x << 1;
x = (x ^ 1);
}
else
x = x << 1;
}
}
return x;
}
Start honing your debugging skills now. If you are going to be any form of an engineer, you'll need to write programs of some variety, and will thus be debugging all of your life.
A simple way to start debugging is to put print statements in to see how far your code makes it before it dies. I recommend you start by isolating the error.
Not sure about the seg fault, but I think
if((x & 128) == 1)
should be
if((x & 128) == 128)
or just
if(x & 128)
I tried on my computer (MacBookPro / Core2Duo) and it worked.
By the way, what's your target architecture ? Some (many) processors perform rotation instead of shifts when you use the C operators ">>" and "<<".
When you use ^ don't you mean the or operator | ?
Related
Dear all C programmer:
X = 1 << N; (left shift)
how to recover N from X ?
Thanks
N in this case is the bit position where you shifted in a 1 at. Assuming that X here only got one bit set. Then to find out what number that bit position corresponds to, you have to iterate through the data and mask with bitwise AND:
for(size_t i=0; i<sizeof(X)*8; i++)
if(X & (1<<i))
printf("%d", i);
If performance is important, then you'd make a look-up table with all possible results instead.
In a while loop, keep shifting right until X==1, record how many times you have to shift right and the counter will give you N.
int var = X;
int count = 0;
while (var != 1){
var >>= 1;
count++;
}
printf("N is %d", count);
Try this (flsl from here which is available from string.h on macOS) :
int flsl(long mask)
{
int bit;
if (mask == 0) return (0);
for (bit = 1; mask != 1; bit++)
mask = (unsigned long)mask >> 1;
return (bit);
}
unsigned char binlog(long mask) { return mask ? flsl(mask) - 1 : 0; }
int x = 1 << 20;
printf("%d\n", binlog(x)); ===> 20
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.
I was trying to make a simple function to check how many bits set to 1 were in an int.
What I achieved at first was
#include <stdio.h>
int bitsOne (int x){
int r=0;
while (x > 0){
if (x % 2 == 1) r++;
x = x/2;
}
return r;
}
I was trying to use the >> operator for this instead but I don't know how I can store the shifted number.
Update
Using Brick's suggestion I achieved what I wanted,
#include <stdio.h>
int bitsOne (int x){
int r=0;
int bit;
while (x > 0){
bit = (x & 1);
if (bit == 1) r++;
x>>=1;
}
return r;
}
Get the bit in the last slot before you do the shift using a mask:
int bit = (x & 1);
Then do the shift on x.
>> is an operator like any other. x = x>>1; will do it, which of course means x>>=1 will do it.
Update
if you want to divide by 2 you should indeed shift by 1.
Don't make fun of me, I'm old.
Let's say we have a variable x of 64 bits, we also know how many bits of such variable we actually use, say 1 <= nx <= 64 , so the last bit is in position nx - 1. What could be the fastest way to perform a padding of the last bit in the remaining 64 - nx?
I would try something like (pseudocode/C):
uint64_t padd_input(uint64_t x, int nx) {
assert(0 < nx && nx <= 64);
msb = (x & (1ULL<<(nx - 1))) != 0ULL; //or (x >> (nx - 1)) & 0x1ULL;
x |= ((msb<<(64 - nx)) - msb)<<nx;
return x;
}
Is all the shift/masking redundant? Or is there a smarter way to achieve the same thing?
I make an example of I want to achieve, assume the unused part is already set to 0.
Let's say i have 0x7, and nx = 4 in this case there's nothing to do. Assuming instead 0xF the padding has to provide 0xFFFFFFFFFFFFFFFF.
I would do:
uint64_t padd_input(uint64_t x, int nx)
{
uint64_t t = x & (1ULL << (nx-1));
t = t - 1;
x = x | ~t;
return x;
}
or perhaps
uint64_t padd_input(uint64_t x, int nx)
{
uint64_t t = x & (1ULL << (nx-1));
if (t)
{
t = t - 1;
x = x | ~t;
}
return x;
}
as it seems more clear to me.
Note: I have not compared performance of OPs code and my code.
Given an unsigned integer a (less or equal to 1024), I need to find a number p which satisfy the following condition :
lowest p >= a
p is a power of 2
I'm sure there is a better solution, using bitwise operators.
Have you a better solution ?
unsigned int closest_pow2(unsigned int a)
{
if (a == 0 || a > 1024) return 0; //error, never happen
if (a == 1) return 1;
if (a == 2) return 2;
if (a <= 4) return 4;
if (a <= 8) return 8;
if (a <= 16) return 16;
if (a <= 32) return 32;
if (a <= 64) return 64;
if (a <= 128) return 128;
if (a <= 256) return 256;
if (a <= 512) return 512;
if (a <= 1024) return 1024;
}
The following does it without the relatively expensive conditional statements or loops:
unsigned next_power_of_two(unsigned int x) {
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
return x + 1;
}
If this is a trick question (since I see no requirement that you must find the lowest p >= a), then this is a solution:
return 1024;
Stylistically, I prefer not to use bitwise operators because they tend to make the code harder to read--they encapsulate the bit structure much less than other types of commands. Even without bitwise operators, the code could be made much more concise:
int pow = 1;
if (a == 0 || a > 1024) return 0;
while (pow < 2000) {
if (a <= pow) return pow;
pow *= 2;
}
If you don't want to hardcode a number larger than the largest number of bits (probably a better coding practice anyway), you can write as follows:
final int MAX_POSSIBLE_BIT_VALUE = 1024;
unsigned int closest_pow2(unsigned int a) {
if (a == 0 || a > MAX_POSSIBLE_BIT_VALUE) return 0;
int pow = 1;
while (pow <= MAX_POSSIBLE_BIT_VALUE) {
if (a <= pow) return pow;
pow *= 2;
}
return pow;
}