Related
I'm looking for a way to swap 2 bits at a given position but counting starts from MSB(most significant bit) to LSB (least significant bit).
Lets say i have position p1 = 0 and p2 = 2 and my number is 1000 = 8 . The result should be
0010 .
I tried this piece of code but it swaps the bits starting from LSB to MSB . How do I "reverse" the process?
unsigned int bit1 = (num >> p1) & 1;
unsigned int bit2 = (num >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
result = num ^ x;
By your example, I will assume you mean to start numbering bits from the MSB of the number value. That is, given:
00010100
↑
msb == most significant bit in the number
In that case, you need a way to count the bit index of the MSB. There are actually processor instructions you can use to do that, and both MSVC and GCC (and Clang) provide special functions to access that functionality...
...but you don’t need that. Instead, just write yourself a function:
int index_of_msb( unsigned long value )
{
...
}
Remember that you can shift a number down by one bit at a time. As long as the number is not zero, you have at least one set bit left.
1 0 1 0 0 0
→ 1 0 1 0 0 (1 shift)
→ 1 0 1 0 (2 shifts)
→ 1 0 1 (3 shifts)
→ 1 0 (4 shifts)
→ 1 (5 shifts)
→ 0
Five shifts → bit 5 is MSB.
Now you can convert your bit positions to the standard shift offsets.
p1 = p_msb - p1;
p2 = p_msb - p2;
What remains to do is use your standard bit operators to swap the two bit values.
Four bits is small enough that you can use a 256-byte table:
unsigned char SwapBits(unsigned char number, int p0, int p1)
{
static const unsigned char Table[16][4][4] = {
{ { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 1, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 2, 1, 0, 0}, { 1, 2, 2, 2}, { 0, 2, 2, 2}, { 0, 2, 2, 2} },
{ { 3, 3, 1, 1}, { 3, 3, 2, 2}, { 1, 2, 3, 3}, { 1, 2, 3, 3} },
{ { 4, 2, 1, 0}, { 2, 4, 4, 4}, { 1, 4, 4, 4}, { 0, 4, 4, 4} },
{ { 5, 3, 5, 1}, { 3, 5, 6, 5}, { 5, 6, 5, 4}, { 1, 5, 4, 5} },
{ { 6, 6, 3, 2}, { 6, 6, 5, 4}, { 3, 5, 6, 6}, { 2, 4, 6, 6} },
{ { 7, 7, 7, 3}, { 7, 7, 7, 5}, { 7, 7, 7, 6}, { 3, 5, 6, 7} },
{ { 8, 4, 2, 1}, { 4, 8, 8, 8}, { 2, 8, 8, 8}, { 1, 8, 8, 8} },
{ { 9, 5, 3, 9}, { 5, 9, 9, 12}, { 3, 9, 9, 10}, { 9, 12, 10, 9} },
{ {10, 6, 10, 3}, { 6, 10, 12, 10}, {10, 12, 10, 9}, { 3, 10, 9, 10} },
{ {11, 7, 11, 11}, { 7, 11, 13, 14}, {11, 13, 11, 11}, {11, 14, 11, 11} },
{ {12, 12, 6, 5}, {12, 12, 10, 9}, { 6, 10, 12, 12}, { 5, 9, 12, 12} },
{ {13, 13, 7, 13}, {13, 13, 11, 13}, { 7, 11, 13, 14}, {13, 13, 14, 13} },
{ {14, 14, 14, 7}, {14, 14, 14, 11}, {14, 14, 14, 13}, { 7, 11, 13, 14} },
{ {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15} },
};
return Table[number][p0][p1];
}
I've created a code to calculate my class notes, it was working the first time, but after some runs, it shows:
[Error] variable-sized object may not be initialized
I don't know why! :(
CODE:
system("Color Fs");
float total = 0, moyenne;
int i,j, mawad = 11, ghanat = 3;
char * NAMES[] = {"Archi", "Dev", "EEJ", "ENG", "AR", "FR", "MATH", "RESEAU", "LINUX", "WIN", "TEC"};
float NOTES[mawad][ghanat] =
{
{20, 0, 0},
{20, 0, 0},
{10, 0, 0},
{10, 0, 0},
{10, 0, 0},
{10, 0, 0},
{15, 0, 0},
{35, 0, 0},
{30, 0, 0},
{30, 0, 0},
{10, 0, 0},
};
printf("------------------------------ LAY YSBR LGAMI3 ------------------------------\n");
for (i=0;i<mawad;i++){
printf("DGHL NO9TA DYAL %s", NAMES[i]);
for (j=0;j<2;j++) {
do {
printf("\t\tFRD %d:\n", j+1);
scanf("%f", &NOTES[i][j+1]);
} while (NOTES[i][j+1] > 20); // ILA DGHL KTR MN 20;
}
total += ((NOTES[i][1] + NOTES[i][2]) / 2) * NOTES[i][0];
}
moyenne = total / 200;
printf("\n\tMoyenne: %.3f \n", moyenne);
To fix that we need to add values directly:
float NOTES[11][3] =
{
{20, 0, 0},
{20, 0, 0},
{10, 0, 0},
{10, 0, 0},
{10, 0, 0},
{10, 0, 0},
{15, 0, 0},
{35, 0, 0},
{30, 0, 0},
{30, 0, 0},
{10, 0, 0},
};
I have an object that looks like this:
{id: 1, cells: ["Ashar", 68345, 14, 66666, "Retail", 1, ""]}
And I have an array of objects like so:
[
{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]},
{id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]},
{id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]},
{id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]},
{id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
]
What I am trying to do is take the id of the first object and use it to search through the array to find the matching id and then replace the value of cells with the value from the first object.
Any help with figuring this out would be much appreciated.
Thanks for your time.
var temp = {id: 1, cells: ["Ashar", 68345, 14, 66666, "Retail", 1, ""]};
var arr=[
{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]},
{id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]},
{id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]},
{id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]},
{id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
];
arr.forEach(function(value,key){
if(value.id == temp.id) {
value.cells=temp.cells;
}
});
I'm using angular-nvd3 and I'm having a lot of trouble figuring out how to change the way things animate.
Below is what currently happens when you toggle a dataset on. As you can see the Expenses animate in from the upper right and the Distances animate in directly from the right. What I would like them both to do is one of two things. Either animate with a basic fade in, or grow upward.
Here is my controller. Interestingly when I added yDomain1: [1, 200] it caused the Distance chart to animate directly from the right, prior to that it was also animating in from the upper right.
controllers.controller("DashboardController", ['$scope','MileageRestService',
($scope,MileageRestService)->
$scope.vehicle_log_data = {}
MileageRestService.dashboard(
(result)->
console.log('dashboard result', result)
$scope.vehicle_log_data = result
distances = result.current_year.chart_data.distances
expenses = result.current_year.chart_data.expenses
$scope.data = [
{
key: "Distances",
color: '#ff7f0e',
area: true,
type: "line",
yAxis: 1,
values: distances
},
{
key: "Expenses",
color: '#7777ff',
area: true,
type: "line",
yAxis: 2,
values: expenses
}
]
$scope.options = {
chart: {
type: 'multiChart',
height: 256,
margin : {
top: 20,
left: 50
},
x: (d)-> d.x,
y: (d)-> (d.y),
showValues: true,
lines1: {
duration: 500
},
lines2: {
duration: 500
},
yDomain1: [1, 200],
transitions: true,
useInteractiveGuideline: true,
clipEdge: false
}
}
(error)->
console.log('dashboard error', error)
)
])
The Distance and Expense data is:
distance = [
{x: 1, y: 100},
{x: 2, y: 25},
{x: 3, y: 150},
{x: 4, y: 110},
{x: 5, y: 0},
{x: 6, y: 175},
{x: 7, y: 0},
{x: 8, y: 0},
{x: 9, y: 0},
{x: 10, y: 0},
{x: 11, y: 0},
{x: 12, y: 0}
]
expenses = [
{x: 1, y: 10},
{x: 2, y: 5},
{x: 3, y: 15},
{x: 4, y: 7.75},
{x: 5, y: 0},
{x: 6, y: 20},
{x: 7, y: 0},
{x: 8, y: 0},
{x: 9, y: 0},
{x: 10, y: 0},
{x: 11, y: 0},
{x: 12, y: 0}
]
I have a piece of code that runs on an embedded system. Its job is to convert some ASCII characters into proprietary data. The data is stored in a multi-dimensional array and what appears to be the problem, although I am unable to confirm this with the hardware debugger, is that the bit variable is staying at value 2. This code works the first two times it is run, but on the third run it breaks and returns starts sending wrong data through the UART interface. I though maybe someone else analyzing this might be able to see what I'm missing. This is C99 which I am not too familiar with. Bleow is the entire function, but I think the problem is with the for statement?? Any help would be greatly appreciated!
void simple_uart_putstring(uint8_t *str, uint16_t length)
{
//send data bits
uint_fast8_t index = 0;
uint8_t ch = str[index++];
uint_fast8_t bitCount = 0;
int index2 = 0;
int bit = 0;
if (length > 1)
{
while (length >= index)
{
if (bitCount < 2)
{
if (length < 10)
{
//send sync bits
simple_uart_put(254);
simple_uart_put(223);
bitCount = 2;
} else {
//send sync bits and add scrolling
simple_uart_put(254);
simple_uart_put(222);
bitCount = 2;
}
}
//send each bit for each letter in the string
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch;
bit = (int)i;
simple_uart_put(matrix[index2 - 32][bit]);
bitCount++;
}
ch = str[index++];
}
//the main controller is expecting 150 bits total to continue to send bit until 150
while (bitCount <= 150)
{
simple_uart_put(0);
bitCount++;
if(bitCount >= 150)
{
bitCount = 0;
break;
}
}
bitCount = 0;
}
}
And here is a sample of the array:
const uint8_t matrix[59][5] =
{
{ 0, 0, 0, 0, 0}, //space
{ 0, 125, 0, 0, 0}, //!
{ 0, 112, 0, 112, 0}, //"
{127, 127, 127, 127, 127 }, //#
{ 18, 42, 107, 36, 0}, //$
{ 50, 52, 22, 38, 0}, //%
{ 38, 89, 57, 6, 9}, //&
{ 64, 48, 0, 0, 0}, //'
{ 0, 0, 62, 65, 0}, //(
{ 0, 65, 62, 0, 0}, //)
{ 20, 8, 62, 8, 20}, //*
{ 0, 8, 28, 8, 0}, //+
{ 1, 6, 0, 0, 0}, //,
{ 0, 8, 8, 8, 0}, //-
{ 3, 3, 0, 0, 0}, //.
{ 2, 4, 8, 16, 32}, //
{ 62, 69, 73, 62, 0}, //0
{ 1, 33, 127, 1, 0}, //1
{ 35, 67, 69, 49, 0}, //2
{ 34, 73, 73, 54, 0}, //3
{ 12, 20, 36, 127, 0}, //4
{ 114, 81, 81, 78, 0}, //5
{ 30, 41, 73, 6, 0}, //6
{ 64, 71, 72, 112, 0}, //7
{ 54, 73, 73, 54, 0}, //8
{ 48, 73, 74, 60, 0}, //9
{ 0, 54, 54, 0, 0}, //:
{ 0, 1, 54, 0, 0}, //;
{ 0, 8, 20, 34, 0}, //<
{ 0, 20, 20, 20, 0}, //=
{ 0, 34, 20, 8, 0}, //>
{ 32, 64, 69, 72, 48}, //?
{ 62, 65, 93, 93, 112}, //#
{ 63, 72, 72, 63, 0 }, //a
{ 127, 73, 73, 54, 0 }, //b
{ 62, 65, 65, 34, 0}, //c
{ 127, 65, 34, 28, 0 }, //d
{ 127, 73, 73, 65, 0}, //e
{ 127, 72, 72, 64, 0}, //f
{ 62, 65, 73, 47, 0}, //g
{ 127, 8, 8, 127, 0}, //h
{ 0, 65, 127, 65, 0},//i
{ 6, 65, 126, 64, 0}, //j
{ 127, 8, 20, 99, 0}, //k
{ 127, 1, 1, 1, 0}, //l
{ 127, 32, 24, 32, 127}, //m
{ 127, 16, 8, 127, 0}, //n
{ 62, 65, 65, 62, 0}, //o
{ 127, 72, 72, 48, 0}, //p
{ 60, 70, 66, 61, 0}, //q
{ 127, 76, 74, 49, 0}, //r
{ 50, 73, 73, 38, 0}, //s
{ 0, 64, 127, 64, 0}, //t
{ 126, 1, 1, 126, 0},
{ 127, 1, 2, 124, 0},
{ 126, 1, 6, 1, 126},
{ 99, 28, 28, 99, 0},
{ 112, 8, 8, 127, 0},
{ 71, 73, 81, 97, 0}};
And the uart send method:
void simple_uart_put(uint8_t cr)
{
NRF_UART0->TXD = (uint8_t)cr;
while (NRF_UART0->EVENTS_TXDRDY!=1)
{
// Wait for TXD data to be sent
}
NRF_UART0->EVENTS_TXDRDY=0;
}
An example of this working would be if the input string is "AB" and the length = 2;
It should send the following bytes via UART:
{254, 223, 63, 72, 72, 63, 0, 127, 73, 73, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, etc....}
The first two bytes are sync bytes, the next five come from the array matrix[33][0 to 4] because ASCII 'A' = 65 and 65-32 = 33. Then the next five come from ASCII 'B' = 66 and 66 - 32 = 34 so they are sent from matrix[34][0 to 4]. Then the next n = 150 - bitNumber are sent as 0 because the main controller is expecting 150 bytes always.
See edit at bottom
Without having definitions for all functions, I cannot completely analyze this, however there are a couple of suspicious things:
1) this declaration is odd:
uint_fast8_t index = 0;
uint8_t ch = str[index++]; //always sets ch to first character of input "str" then increments index.
NOTE: corrected comment on previous line.
2) although the comment indicates "each bit for each letter in the string" it only handle 5:
for (uint_fast8_t i = 0; i < 5; i++){...} //what if lenth of input is less than 5?
Suggest changing to:
for (uint_fast8_t i = 0; i < length; i++){...} //used second argument "length"
3) Finally, it seems that null terminating the string should follow the for loop, but: (see comments in line) (Also, input arguments used were "abc", 4)
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch; //ch inits to first char in str, and indexes through
bit = (int)i;
//simple_uart_put(matrix[index2 - 32][bit]);
bitCount++;
}
ch = str[index++]; //terminates in second character of input "b' in this case
//I think this should null terminate with '\0' if it is to be treated as a C string,
//but because, as you say, this is "proprietary", I am not sure.
EDIT
I think the problem may be that you have declared the variable matrix with what looks like sufficient room, but only initialized it with three rows of data:
const uint8_t matrix[59][5] =
{
{ 0, 0, 0, 0, 0}, //space
{ 0, 125, 0, 0, 0}, //!
{ 0, 112, 0, 112, 0}, //"
}; //have only initialized matrix[0], matrix[1] and matrix[2]
The remaining 53 rows of data, although owned by you, have not been initialized that I can see. So, when you say:
the next five come from the array matrix[33][0 to 4] because ASCII 'A' = 65 and 65-32 = 33. Then the next five come from ASCII 'B' = 66 and 66 - 32 = 34 so they are sent from matrix[34][0 to 4]
That suggests what ever random number happens to be occupying those uninitialized memory locations, are what is being written out in the line:
simple_uart_put(matrix[index2 - 32][bit]);
EDIT 2 (See comment to explain)
Here is my main(), and commented simple_uart_putstring():
int main()
{
uint8_t *str;
int len=3;
str = malloc(3); //extra char for terminating null byte
strcpy(str, "AB");
simple_uart_putstring(str, 2);
free(str);
}
void simple_uart_putstring(uint8_t *str, uint16_t length)
{
//send data bits
uint_fast8_t index = 0;
uint8_t ch = str[index++]; //ch inits to first char in str, and indexes through
uint_fast8_t bitCount = 0;
int index2 = 0;
int bit = 0;
if (length > 1)
{
while (length >= index)
{
if (bitCount < 2)
{
if (length < 10)
{
//send sync bits
//simple_uart_put(254);
//simple_uart_put(223);
bitCount = 2;
} else {
//send sync bits and add scrolling
//simple_uart_put(254);
//simple_uart_put(222);
bitCount = 2;
}
}
//send each bit for each letter in the string
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch; //
bit = (int)i;
/*simple_uart_put(*/matrix[index2 - 32][bit];//);break here to view "matrix[index2 - 32][bit]"
bitCount++;
}
ch = str[index++]; //
}
//the main controller is expecting 150 bits total to continue to send bit until 150
while (bitCount <= 150)
{
//simple_uart_put(0);
bitCount++;
if(bitCount >= 150)
{
bitCount = 0;
break;
}
}
bitCount = 0;
}
}