# Total function between two elements of sub_lists

We have a list containing IntegerDigits elements as

list0={{0,0,1,0,0,0},
{0,0,1,0,1,0},
{0,0,1,1,0,0},
{0,0,1,1,1,0},
{0,1,1,0,0,0},
{0,1,1,0,1,0},
{0,1,1,1,1,0},
{1,0,1,0,0,0},
...};


Two any arbitrary positions of each element can be definitely determined by 0 or 1. For instants, in the above list, we have 0 in the first position and 1 in the fourth position. They are fixed and are not changeable. We have to create a list whose elements are -1 or +1. If the Total between two mentioned positions isEven (if evenQ=True) it must be +1, otherwise it must be -1. Or we can say (-1)^(Total[elements between first and fourth positions]) I mean:

list1={{1}, (*0+0=0*)
{-1}, (*0+1=1*)
{-1}, (*1+0=1*)
{+1},  (*1+1=2*)
{+1},
{-1},
{+1},
{+1},
....}


How can I do this aim?

• Why do you write (*0+0=0*) for the first one? It has 1 in the 3rd position, and 0 in the 6th position. Aug 17, 2017 at 16:23
• Should the position be taken backwards because IntegerDigits puts the coefficient of the smallest power of the base at the end of the output list Aug 17, 2017 at 16:25
• Something like (-1)^Total[list0[[All, {4, 5}]], {2}]? Aug 17, 2017 at 16:26
• For your second comment you are right. So sorry. But for the first comment: there are two 0s between 1 and 0. Aug 17, 2017 at 16:27
• So sorry I corrected them. Aug 17, 2017 at 16:29

list0 =
{{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 1, 0}, {0, 0, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 0}, {0, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 0},
{0,1, 1, 1, 1, 0}, {1, 0, 1, 0, 0, 0}};

If[#, 1, -1]& @* EvenQ @* Total /@ list0[[All, 4 ;; 5]]


{1, -1, -1, 1, 1, -1, 1, 1}

Or, similar to Carl Woll's comment,

(-1)^(Plus @@@ list0[[All, 4 ;; 5]])

• Thank you so much but because the positions are arbitrary the Total maybe exceeds from 2 or 0 Aug 17, 2017 at 16:31
– eldo
Aug 17, 2017 at 16:41

I suspect you started out with the integers themselves:

ints = FromDigits[#, 2] & /@ list0


{8, 10, 12, 14, 24, 26, 30, 40}

If that's the case, then you don't need IntegerDigits:

(-1)^Sign[Mod[BitAnd[2^1 + 2^2, #], 2^1 + 2^2]] & /@ ints
(-1)^Sign[Mod[BitAnd[6, #], 6]] & /@ ints


{1, -1, -1, 1, 1, -1, 1, 1}

where 2^0 corresponds to the last element of IntegerDigits and 2^1 corresponds to the second last, etc.

• What is dd? It was not defined! Aug 17, 2017 at 16:34
• @InzoBabaria I removed them Aug 17, 2017 at 16:35
• I cannot understand why 6?! Aug 17, 2017 at 16:39
• I cannot generalize to an arbitrary case Aug 17, 2017 at 16:39
• You might be interested in BitGet[]. Aug 17, 2017 at 16:49