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];
}
Very new to Swift
I have a multidimensional array of some 500 records
[10, 2, 4, 10, 23, 56]
[0, 12, 14, 20, 28, 42]
[0, 2, 4, 10, 26, 54]
[1, 24, 34, 40, 47, 51]
[1, 23, 24, 30, 33, 50]
so that I would have
[0, 2, 4, 10, 26, 54]
[0, 12, 14, 20, 28, 42]
[1, 23, 24, 30, 33, 50]
[1, 24, 34, 40, 47, 51]
[10, 2, 4, 10, 23, 56]
I am fine for the individual record sort.
But when looking at the 500 records, to sort the records for the first column I used
arr.sort { $0[0] < $1[0] }.
which worked fine, I need to extend that to columns 2,3,4,5,6. I want to be able to sort on Column 1 then by 2, by 3, by 4, by 5, by 6.
Assuming that all subarrays contains 6 elements you can use a tuple (which conforms to Comparable to an arity of 6) to sort your array:
let array = [[10, 2, 4, 10, 23, 56],
[0, 12, 14, 20, 28, 42],
[0, 2, 4, 10, 26, 54],
[1, 24, 34, 40, 47, 51],
[1, 23, 24, 30, 33, 50]]
let sorted = array.sorted(by: {
($0[0],$0[1],$0[2],$0[3],$0[4],$0[5]) < ($1[0],$1[1],$1[2],$1[3],$1[4],$1[5])
})
print(sorted) // [[0, 2, 4, 10, 26, 54],
// [0, 12, 14, 20, 28, 42],
// [1, 23, 24, 30, 33, 50],
// [1, 24, 34, 40, 47, 51],
// [10, 2, 4, 10, 23, 56]]
How can I rewrite all values in array? For example I have array:
int[] polyX = { -22, 21, 166, 174, 106, 33, 20, 14, -30, -19, -24 };
And now I want to set these values:
polyX = { -43, 5, 23, 65, -64, 33, 4, 14, -30};
Is it possible to set new values to arrays like to any other variable?
There's a syntax error in your second line. It should actually be:
polyX = new[] { -43, 5, 23, 65, -64, 33, 4, 14, -30, -3, -321};
Alright so I am new here so please be easy on me :)
I am currently developing on a uC in C and using linked lists to create structures every time the user wants to create one. Example:
typedef struct {
char* dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
}listelement;
listelement * AddItem (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[L][Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
// move down to the end of the list
while (listpointer -> link != NULL)
listpointer = listpointer -> link;
listPointerTemp = listpointer;
listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
// on fail end links becomes NULL already above
if(listpointer -> link != NULL){
listpointer = listpointer -> link;
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return lp;
}
else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
if(listpointer != NULL){
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc (sizeof(name));
if(listpointer -> dataitem != NULL){
for(int16_t i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return listpointer;
}
}
So now I want to add structures already stored in memory using const as such:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
const char dataitem1[4] = {'T','r','o','n'};
const int16_t wordSize1 = 10, nameSize = 4;
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement Tron = { dataitem1, NULL, wordSize1, libWord1, 0x00 };
Alright so even if you skip reading all of that, I was just wondering what a good way of somehow create constant structs (do I have to create a new structure format just for const types?) and then link the beginning of the RAM linked lists when user creates first one to the Flash portion of linked lists created on startup?
Let me know if more information is needed. I tried not to put too much code on here.
Thanks!
So here is the change and the effects:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// ON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord2[10][Q] = { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement* Tron = { "Tron", NULL, 10, libWord1, 0x00 };
// next element links to Tron . . .
const listelement* On = { "On", Tron, 10, libWord2, 0x01 };
// next element links to On . . . and so on . . .
listelement* constLinkInit(void){
return On;
}
I know I didn't do your global array of structs but ignoring that and passing the last struct to this guy in a nother file using a function.
listelement *listpointer;
// initially listpointer is initialized to NULL but now set to end of link list of FLASH
listpointer = constLinkInit();
And the going to print links like I had been doing for user defined structs dynamically (which had worked) fails when pointing to the next link even though the address passed to listpointer (On) is correct.
void PrintQueue (listelement * listpointer) {
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
if (listpointer == NULL){
usart_write_line(&AVR32_USART0,"queue is empty!\r\n");
}else{
//usart_write_line(&AVR32_USART0," Word List \r\n");
//usart_write_line(&AVR32_USART0,"--------------------------------------------\r\n");
while (listpointer != NULL) {
//usart_write_line(&AVR32_USART0, listpointer -> dataitem);
//usart_write_line(&AVR32_USART0,"\r\n");
//writeCOM_int_array(listpointer -> libWord , listpointer -> wordSize);
listpointer = listpointer -> link; // <---- FAILS HERE
}
}
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
When pointing to the next link it goes to the wrong address . . .
Thanks for the help!
Something like this should work:
#include <stdlib.h>
#include <stdint.h>
#define Q 5
typedef struct {
char *dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
} listelement;
int16_t zzz[Q] = { 11, 22,33,44,55};
listelement global_array[] =
{{ "Sron", global_array+1, 2, zzz , 0 }
,{ "Tron", global_array+2, 2, zzz , 0 }
,{ "Uron", global_array+3, 2, zzz , 0 }
,{ "Vron", NULL , 2, zzz , 0 }
};
i know this sounds silly, but can someone please post the arrays described by rfc2612:
Cm = 0x5A827999
Mm = 0x6ED9EBA1
Cr = 19
Mr = 17
for (i=0; i<24; i++)
{
for (j=0; j<8; j++)
{
Tmj_(i) = Cm
Cm = (Cm + Mm) mod 2**32
Trj_(i) = Cr
Cr = (Cr + Mr) mod 32
}
}
i think im doing is wrong for some reason
i get this for Tr
[[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2]]
Tr0 = { 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11 }
Tr1 = { 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28 }
Tr2 = { 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13 }
Tr3 = { 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30 }
Tr4 = { 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15 }
Tr5 = { 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0 }
Tr6 = { 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17 }
Tr7 = { 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2 }
..and Tm (in hex):
Tm0 = { 5a827999, d151d6a1, 482133a9, bef090b1, 35bfedb9, ac8f4ac1, 235ea7c9, 9a2e04d1, 10fd61d9, 87ccbee1, fe9c1be9, 756b78f1, ec3ad5f9, 630a3301, d9d99009, 50a8ed11, c7784a19, 3e47a721, b5170429, 2be66131, a2b5be39, 19851b41, 90547849, 723d551 }
Tm1 = { c95c653a, 402bc242, b6fb1f4a, 2dca7c52, a499d95a, 1b693662, 9238936a, 907f072, 7fd74d7a, f6a6aa82, 6d76078a, e4456492, 5b14c19a, d1e41ea2, 48b37baa, bf82d8b2, 365235ba, ad2192c2, 23f0efca, 9ac04cd2, 118fa9da, 885f06e2, ff2e63ea, 75fdc0f2 }
Tm2 = { 383650db, af05ade3, 25d50aeb, 9ca467f3, 1373c4fb, 8a432203, 1127f0b, 77e1dc13, eeb1391b, 65809623, dc4ff32b, 531f5033, c9eead3b, 40be0a43, b78d674b, 2e5cc453, a52c215b, 1bfb7e63, 92cadb6b, 99a3873, 8069957b, f738f283, 6e084f8b, e4d7ac93 }
Tm3 = { a7103c7c, 1ddf9984, 94aef68c, b7e5394, 824db09c, f91d0da4, 6fec6aac, e6bbc7b4, 5d8b24bc, d45a81c4, 4b29decc, c1f93bd4, 38c898dc, af97f5e4, 266752ec, 9d36aff4, 14060cfc, 8ad56a04, 1a4c70c, 78742414, ef43811c, 6612de24, dce23b2c, 53b19834 }
Tm4 = { 15ea281d, 8cb98525, 388e22d, 7a583f35, f1279c3d, 67f6f945, dec6564d, 5595b355, cc65105d, 43346d65, ba03ca6d, 30d32775, a7a2847d, 1e71e185, 95413e8d, c109b95, 82dff89d, f9af55a5, 707eb2ad, e74e0fb5, 5e1d6cbd, d4ecc9c5, 4bbc26cd, c28b83d5 }
Tm5 = { 84c413be, fb9370c6, 7262cdce, e9322ad6, 600187de, d6d0e4e6, 4da041ee, c46f9ef6, 3b3efbfe, b20e5906, 28ddb60e, 9fad1316, 167c701e, 8d4bcd26, 41b2a2e, 7aea8736, f1b9e43e, 68894146, df589e4e, 5627fb56, ccf7585e, 43c6b566, ba96126e, 31656f76 }
Tm6 = { f39dff5f, 6a6d5c67, e13cb96f, 580c1677, cedb737f, 45aad087, bc7a2d8f, 33498a97, aa18e79f, 20e844a7, 97b7a1af, e86feb7, 85565bbf, fc25b8c7, 72f515cf, e9c472d7, 6093cfdf, d7632ce7, 4e3289ef, c501e6f7, 3bd143ff, b2a0a107, 296ffe0f, a03f5b17 }
Tm7 = { 6277eb00, d9474808, 5016a510, c6e60218, 3db55f20, b484bc28, 2b541930, a2237638, 18f2d340, 8fc23048, 6918d50, 7d60ea58, f4304760, 6affa468, e1cf0170, 589e5e78, cf6dbb80, 463d1888, bd0c7590, 33dbd298, aaab2fa0, 217a8ca8, 9849e9b0, f1946b8 }
(I'm not sure why they didn't just include these as tables in the spec).