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 am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}
I am writing code where five random numbers are given a value. Those five values are then sorted in an array in numerical order. However, when one value is equaled to 1, I want that number to be in the middle of the sorted array.
When there is no number 1, the sorted array will work correctly.
let fourthRandomNumber:Int = Int(arc4random_uniform(52))
let fifthRandomNumber:Int = Int(arc4random_uniform(52))
let sixthRandomNumber:Int = Int(arc4random_uniform(52))
let seventhRandomNumber:Int = Int(arc4random_uniform(52))
let eighthRandomNumber:Int = Int(arc4random_uniform(52))
var loFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var hiFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
let loFourthRandomNumber:Int = loFourthRandomNumberOutput [fourthRandomNumber]
let loFifthRandomNumber:Int = loFifthRandomNumberOutput [fifthRandomNumber]
let loSixthRandomNumber:Int = loSixthRandomNumberOutput [sixthRandomNumber]
let loSeventhRandomNumber:Int = loSeventhRandomNumberOutput [seventhRandomNumber]
let loEighthRandomNumber:Int = loEighthRandomNumberOutput [eighthRandomNumber]
let hiFourthRandomNumber:Int = hiFourthRandomNumberOutput [fourthRandomNumber]
let hiFifthRandomNumber:Int = hiFifthRandomNumberOutput [fifthRandomNumber]
let hiSixthRandomNumber:Int = hiSixthRandomNumberOutput [sixthRandomNumber]
let hiSeventhRandomNumber:Int = hiSeventhRandomNumberOutput [seventhRandomNumber]
let hiEighthRandomNumber:Int = hiEighthRandomNumberOutput [eighthRandomNumber]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var loDealerCardsArray = [loFourthRandomNumberOutput [fourthRandomNumber], loFifthRandomNumberOutput [fifthRandomNumber], loSixthRandomNumberOutput [sixthRandomNumber], loSeventhRandomNumberOutput [seventhRandomNumber], loEighthRandomNumberOutput [eighthRandomNumber]]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var hiDealerCardsArray = [hiFourthRandomNumberOutput [fourthRandomNumber], hiFifthRandomNumberOutput [fifthRandomNumber], hiSixthRandomNumberOutput [sixthRandomNumber], hiSeventhRandomNumberOutput [seventhRandomNumber], hiEighthRandomNumberOutput [eighthRandomNumber]]
loDealerCardsArray.sort()
loDealerCardsArray[0]
loDealerCardsArray[1]
loDealerCardsArray[2]
print (loDealerCardsArray[0] + loDealerCardsArray[1] + loDealerCardsArray[2])
//NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
hiDealerCardsArray.sort()
hiDealerCardsArray[2]
hiDealerCardsArray[3]
hiDealerCardsArray[4]
print (hiDealerCardsArray[2] + hiDealerCardsArray[3] + hiDealerCardsArray[4])
////NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
var DealerCardsArray = [fourthRandomNumber, fifthRandomNumber, sixthRandomNumber, seventhRandomNumber, eighthRandomNumber]
DealerCardsArray.sort()
DealerCardsArray[0]
DealerCardsArray[1]
DealerCardsArray[2]
DealerCardsArray[3]
DealerCardsArray[4]
For example, five cards have a value of [0, 1, 2, 9, 10], and the array is sorted correctly. I am trying to sort the array so that the 1 is ALWAYS in the middle.
When an ace (value of 1 or 11) appears in the sorted array I want it to look as follows: [0, 2, 1, 9, 10]
Thank you in advance for your assistance
Something like:
var cards = [0, 1, 2, 9, 10]
// Get a list of all the aces in the array.
let aces = cards.filter { $0 == 1 || $0 == 11 }
// Now take the aces out of the array.
cards = cards.filter { $0 != 1 && $0 != 11 }
// Sort the array (unnecessary if its definitely already sorted).
cards = cards.sort()
// Re-insert the aces into the middle.
aces.forEach { cards.insert($0, atIndex: cards.count/2) }
// [0, 2, 1, 9, 10]
Might do the trick (haha)!
var dealerCardsArray = [0, 1, 2, 9, 10]
dealerCardsArray.sort()
// Only if the value 1 exists in the array, remove it and add it back in the center position
if let indexOfOne = dealerCardsArray.index(of: 1) {
dealerCardsArray.remove(at: indexOfOne)
dealerCardsArray.insert(1, at: array.count/2)
}
print(dealerCardsArray) // [0,2,1,9,10]
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;
}
});
This may sound somewhat stupid, but I have to know as I'm writing a bingo board in C.
#include <stdio.h>
typedef struct {
int a;
int b;
int c;
int d;
int e;
} row;
typedef struct {
row one;
row two;
row three;
row four;
row five;
} bingo_board;
void initialize_columns()
{
bingo_board board = {
.one = {1, 2, 3, 4, 5},
.two = {6, 7, 8, 9, 10},
.three = {11, 12, 13, 14, 15},
.four = {16, 17, 18, 19, 20},
.five = {21, 22, 23, 24, 25}
};
}
Is this possible?
It can be done simply as
void initialize_columns()
{
bingo_board board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
}
Or even as
void initialize_columns()
{
bingo_board board = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25
};
}
No need to "tag" every row. The tagged syntax is available in C99 though, and what you have in your example is already correct for C99.
Because structs are first class citizens in c, assignment is well defined, this lets you
static bingo_board initial_board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
void init_board(bingo_board *b)
{
*b = initial_board;
}
Which seems to be what you want. If you do declare the board within the function, I would suggest declaring it static, because you don't modify it, so persistent changes are ok, and so that the function doesn't have to grow the stack as much on every call.