Adding binary arrays with different size
I made binary arrays from number and its bit length per line, if 295289042101659 with 6 bits per line, number sizes 49 bit so in array, it would be 6 bits X 9 line, through the code and modified to 6-length zero filled lines:
def listify(a, bit = 5):
res =
while a:
a, b = divmod(a,2**bit)
res.append(b)
return res[::-1]
000001
000011
001001
000001
010110
011101
011110
010110
011011
Since it is binary array, I used binary addition code without carrying:
def binaryadd(one, other):
if one & other:
return False
return one | other
If I get some array of 402(0b110010010) with size 3, then how could I add into the array at point(2,2) by up-to-down coordinate, or (3,6) from down-to-up, right-to-left coordinate?
It should be seemed as:
000001
001111
001101
000101
010110
011101
011110
010110
011011
I've done it like this:
def array_add(one,another, point = (0,0)):
a = [a*2**point[0] for a in another[:]]
a+=[0]*point[1]
a = [0]*(len(one)-len(a))+a
res = [binaryadd(a,b) for a, b in zip(one[::-1],a[::-1])][::-1]
if not all(res):
return False
return res
Is the best way to do it, is to apply binary addition to every values of list, by modifying one list?
Or am I misunderstanding the basics of array?
python arrays numpy binary
add a comment |
I made binary arrays from number and its bit length per line, if 295289042101659 with 6 bits per line, number sizes 49 bit so in array, it would be 6 bits X 9 line, through the code and modified to 6-length zero filled lines:
def listify(a, bit = 5):
res =
while a:
a, b = divmod(a,2**bit)
res.append(b)
return res[::-1]
000001
000011
001001
000001
010110
011101
011110
010110
011011
Since it is binary array, I used binary addition code without carrying:
def binaryadd(one, other):
if one & other:
return False
return one | other
If I get some array of 402(0b110010010) with size 3, then how could I add into the array at point(2,2) by up-to-down coordinate, or (3,6) from down-to-up, right-to-left coordinate?
It should be seemed as:
000001
001111
001101
000101
010110
011101
011110
010110
011011
I've done it like this:
def array_add(one,another, point = (0,0)):
a = [a*2**point[0] for a in another[:]]
a+=[0]*point[1]
a = [0]*(len(one)-len(a))+a
res = [binaryadd(a,b) for a, b in zip(one[::-1],a[::-1])][::-1]
if not all(res):
return False
return res
Is the best way to do it, is to apply binary addition to every values of list, by modifying one list?
Or am I misunderstanding the basics of array?
python arrays numpy binary
Hello. Aboutthe best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code -I've done it like this
- have you? Because this is wierd:self = [0] * (len(block) - len(self)) + self
. Also, what happens tofin_res
inadd
function? Does this code really work?
– Poolka
Nov 13 '18 at 10:21
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (point
and directions likeup-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.
– Poolka
Nov 13 '18 at 14:02
add a comment |
I made binary arrays from number and its bit length per line, if 295289042101659 with 6 bits per line, number sizes 49 bit so in array, it would be 6 bits X 9 line, through the code and modified to 6-length zero filled lines:
def listify(a, bit = 5):
res =
while a:
a, b = divmod(a,2**bit)
res.append(b)
return res[::-1]
000001
000011
001001
000001
010110
011101
011110
010110
011011
Since it is binary array, I used binary addition code without carrying:
def binaryadd(one, other):
if one & other:
return False
return one | other
If I get some array of 402(0b110010010) with size 3, then how could I add into the array at point(2,2) by up-to-down coordinate, or (3,6) from down-to-up, right-to-left coordinate?
It should be seemed as:
000001
001111
001101
000101
010110
011101
011110
010110
011011
I've done it like this:
def array_add(one,another, point = (0,0)):
a = [a*2**point[0] for a in another[:]]
a+=[0]*point[1]
a = [0]*(len(one)-len(a))+a
res = [binaryadd(a,b) for a, b in zip(one[::-1],a[::-1])][::-1]
if not all(res):
return False
return res
Is the best way to do it, is to apply binary addition to every values of list, by modifying one list?
Or am I misunderstanding the basics of array?
python arrays numpy binary
I made binary arrays from number and its bit length per line, if 295289042101659 with 6 bits per line, number sizes 49 bit so in array, it would be 6 bits X 9 line, through the code and modified to 6-length zero filled lines:
def listify(a, bit = 5):
res =
while a:
a, b = divmod(a,2**bit)
res.append(b)
return res[::-1]
000001
000011
001001
000001
010110
011101
011110
010110
011011
Since it is binary array, I used binary addition code without carrying:
def binaryadd(one, other):
if one & other:
return False
return one | other
If I get some array of 402(0b110010010) with size 3, then how could I add into the array at point(2,2) by up-to-down coordinate, or (3,6) from down-to-up, right-to-left coordinate?
It should be seemed as:
000001
001111
001101
000101
010110
011101
011110
010110
011011
I've done it like this:
def array_add(one,another, point = (0,0)):
a = [a*2**point[0] for a in another[:]]
a+=[0]*point[1]
a = [0]*(len(one)-len(a))+a
res = [binaryadd(a,b) for a, b in zip(one[::-1],a[::-1])][::-1]
if not all(res):
return False
return res
Is the best way to do it, is to apply binary addition to every values of list, by modifying one list?
Or am I misunderstanding the basics of array?
python arrays numpy binary
python arrays numpy binary
edited Nov 13 '18 at 13:34
ILoveG11
asked Nov 13 '18 at 8:53
ILoveG11ILoveG11
395
395
Hello. Aboutthe best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code -I've done it like this
- have you? Because this is wierd:self = [0] * (len(block) - len(self)) + self
. Also, what happens tofin_res
inadd
function? Does this code really work?
– Poolka
Nov 13 '18 at 10:21
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (point
and directions likeup-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.
– Poolka
Nov 13 '18 at 14:02
add a comment |
Hello. Aboutthe best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code -I've done it like this
- have you? Because this is wierd:self = [0] * (len(block) - len(self)) + self
. Also, what happens tofin_res
inadd
function? Does this code really work?
– Poolka
Nov 13 '18 at 10:21
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (point
and directions likeup-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.
– Poolka
Nov 13 '18 at 14:02
Hello. About
the best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code - I've done it like this
- have you? Because this is wierd: self = [0] * (len(block) - len(self)) + self
. Also, what happens to fin_res
in add
function? Does this code really work?– Poolka
Nov 13 '18 at 10:21
Hello. About
the best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code - I've done it like this
- have you? Because this is wierd: self = [0] * (len(block) - len(self)) + self
. Also, what happens to fin_res
in add
function? Does this code really work?– Poolka
Nov 13 '18 at 10:21
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (
point
and directions like up-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.– Poolka
Nov 13 '18 at 14:02
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (
point
and directions like up-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.– Poolka
Nov 13 '18 at 14:02
add a comment |
1 Answer
1
active
oldest
votes
Since you mention the numpy tag, you can use it to have high performance and readable code :
import numpy as np
def int_to_array(n,width):
v=np.zeros(64,np.uint8)
i,u,total=0,n,0
while(u):
if i%width == 0 : total += width
u,v[i],i = u//2,u%2,i+1
return v[:total][::-1].reshape(-1,width)
def add(a,b,point=(0,0)):
sx,sy = point
ex = sx+b.shape[0]
ey = sy+b.shape[1]
a[sx:ex,sy:ey] |= b
a=int_to_array(295289042101659,6)
b=int_to_array(402,3)
print(a)
print(b)
add(a,b,(2,2))
print(a)
For :
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 0 0 1]
[0 0 0 0 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
[[1 1 0]
[0 1 0]
[0 1 0]]
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 1 0 1]
[0 0 0 1 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277131%2fadding-binary-arrays-with-different-size%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you mention the numpy tag, you can use it to have high performance and readable code :
import numpy as np
def int_to_array(n,width):
v=np.zeros(64,np.uint8)
i,u,total=0,n,0
while(u):
if i%width == 0 : total += width
u,v[i],i = u//2,u%2,i+1
return v[:total][::-1].reshape(-1,width)
def add(a,b,point=(0,0)):
sx,sy = point
ex = sx+b.shape[0]
ey = sy+b.shape[1]
a[sx:ex,sy:ey] |= b
a=int_to_array(295289042101659,6)
b=int_to_array(402,3)
print(a)
print(b)
add(a,b,(2,2))
print(a)
For :
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 0 0 1]
[0 0 0 0 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
[[1 1 0]
[0 1 0]
[0 1 0]]
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 1 0 1]
[0 0 0 1 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
add a comment |
Since you mention the numpy tag, you can use it to have high performance and readable code :
import numpy as np
def int_to_array(n,width):
v=np.zeros(64,np.uint8)
i,u,total=0,n,0
while(u):
if i%width == 0 : total += width
u,v[i],i = u//2,u%2,i+1
return v[:total][::-1].reshape(-1,width)
def add(a,b,point=(0,0)):
sx,sy = point
ex = sx+b.shape[0]
ey = sy+b.shape[1]
a[sx:ex,sy:ey] |= b
a=int_to_array(295289042101659,6)
b=int_to_array(402,3)
print(a)
print(b)
add(a,b,(2,2))
print(a)
For :
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 0 0 1]
[0 0 0 0 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
[[1 1 0]
[0 1 0]
[0 1 0]]
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 1 0 1]
[0 0 0 1 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
add a comment |
Since you mention the numpy tag, you can use it to have high performance and readable code :
import numpy as np
def int_to_array(n,width):
v=np.zeros(64,np.uint8)
i,u,total=0,n,0
while(u):
if i%width == 0 : total += width
u,v[i],i = u//2,u%2,i+1
return v[:total][::-1].reshape(-1,width)
def add(a,b,point=(0,0)):
sx,sy = point
ex = sx+b.shape[0]
ey = sy+b.shape[1]
a[sx:ex,sy:ey] |= b
a=int_to_array(295289042101659,6)
b=int_to_array(402,3)
print(a)
print(b)
add(a,b,(2,2))
print(a)
For :
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 0 0 1]
[0 0 0 0 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
[[1 1 0]
[0 1 0]
[0 1 0]]
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 1 0 1]
[0 0 0 1 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
Since you mention the numpy tag, you can use it to have high performance and readable code :
import numpy as np
def int_to_array(n,width):
v=np.zeros(64,np.uint8)
i,u,total=0,n,0
while(u):
if i%width == 0 : total += width
u,v[i],i = u//2,u%2,i+1
return v[:total][::-1].reshape(-1,width)
def add(a,b,point=(0,0)):
sx,sy = point
ex = sx+b.shape[0]
ey = sy+b.shape[1]
a[sx:ex,sy:ey] |= b
a=int_to_array(295289042101659,6)
b=int_to_array(402,3)
print(a)
print(b)
add(a,b,(2,2))
print(a)
For :
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 0 0 1]
[0 0 0 0 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
[[1 1 0]
[0 1 0]
[0 1 0]]
[[0 0 0 0 0 1]
[0 0 0 0 1 1]
[0 0 1 1 0 1]
[0 0 0 1 0 1]
[0 1 0 1 1 0]
[0 1 1 1 0 1]
[0 1 1 1 1 0]
[0 1 0 1 1 0]
[0 1 1 0 1 1]]
edited Nov 13 '18 at 14:41
answered Nov 13 '18 at 14:10
B. M.B. M.
13.1k11934
13.1k11934
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
add a comment |
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
It works great except I've supposed the result False (not detecting out one & other), but I'd modify and test the performance. I totally agree it is really readable.
– ILoveG11
Nov 13 '18 at 14:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277131%2fadding-binary-arrays-with-different-size%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hello. About
the best way
- almost any way is viable. It depends on the context and requirements. Right now context and requirements are unknown, rules of addition are unclear. Seems you get what you want with the code -I've done it like this
- have you? Because this is wierd:self = [0] * (len(block) - len(self)) + self
. Also, what happens tofin_res
inadd
function? Does this code really work?– Poolka
Nov 13 '18 at 10:21
well, actually it gets end with return fin_res[::-1], but at first I have to correct the code independent; It has just copied from my class'attribute, so I have to correct all the expressions better to be understood; just wait for a while, please
– ILoveG11
Nov 13 '18 at 10:33
@Poolka I've changed the code. 'a = [0b11111,0b10000,0b10000,0b10000];b = [ob11,0b10,0b10];array_add(a,b) =['0b11111','0b10011','0b10010','0b10010'] - is what I meant
– ILoveG11
Nov 13 '18 at 13:35
Not sure what problem you are trying to solve and what result you expect. The code seems legit. Logic behind the code looks very simple while unclear (
point
and directions likeup-to-down coordinate
). Probably I would use just rows to store binaries in this case to start with. I guess the issue can be solved with numpy more efficiently using literally couple lines of code.– Poolka
Nov 13 '18 at 14:02