0

I have array in javascript like

var arrayHex = [0x50 , 0x4f , 0x53 , 0x54 , 0x20 , 0x2f , 0x20 , 0x48 , 

x54 , 0x54,.. more than 500 hundred elements]

Now I want to do some bit shifting / bits flipping on elements of some slices of above array. For example,

slice(2,6)... slice(30, 50), ... slice (300,400), etc. 

After bit shifting/flipping, send this array (bit shifted/flipped) to my function or do some processing on it. Is it possible ? How could I do this in JavaScript?.

EDITED: Actually, i just want to do some manipulation on original array. Means that if I do operation (i.e. bitwise) on slices of my arrayHex (i.e. from index of 10th-20th, 50th-80th, 230- 450, etc.) then these operation must be reflected in original array not on temporary slices... below is my code

var arrayHex = [0x65 , 0x34 , 0x30 , 0x35 , 0x65];
console.log("before slicing: arrayHex  =  ", arrayHex);
var sliced  = arrayHex.slice(1,3);
for (i=0; i<sliced.length; i++)
{
sliced[i]='0x65';
}
console.log("sliced : ",sliced);
console.log("after slicing arrayHex is : ",arrayHex);

the output is :

before slicing: arrayHex = [ 101, 52, 48, 53, 101 ] sliced : [101, 101 ] after slicing arrayHex is : [ 101, 52, 48, 53, 101 ]

1 Answer 1

1

Yes it is possible. To do it you could iterate through every returned slice's element of your original array. Use biwise operators as you see fit to achieve your goals or do whatever you want on each element. You should note that the operands of any bitwise operator first gets converted to a signed 32 bit integer. You can use this snippet as an outline:

start = 2;
end = 6;
sliced = arrayHex.slice(start, end);
for (i = 0; i < sliced.length(); i++)
{
     //Do stuff on sliced[i]
}

If you want to reflect changes back to your original array then use

slice_index = 0;
for (i = start; i < end; i++)
{
     arrayHex[i] = slice[slice_index];
     slice_index++;
}
slice_index = 0;

If you don't want to get slices in the first place but instead manipulate your array directly then you can do

for (i = 0; i < arrayHex.length(); i++)
{
     //Do stuff on arrayHex[i]
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Javier... but this will done on sliced array... its not reflected back into original array.. I just want to do some manipulations in original array.
Just saw your edit, and edited the answer. Check it out.
thanks for your time.. it seem to be very time consuming (in term of coding)... means i have to make such code for each slices ..... ? Is there any smart way to this ... ?
You can bypass taking slices if you operate directly on your array elements. It's the last snippet in the answer. If you take slices then you have to reflect them back like that. Slices are not meant to be taken, modified and put back again in the array, for this you just modify the elements directly from your array.
Or you can have arrays of slices, but honestly, don't overcomplicate it, keep it simple, don't use slices like that, go straight for the array. You can include the slices snippets above in a loop to iterate over the array of slices. Remember, they are snippets, if you need full code solution let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.