# Re: bin Operator Questions

**URL:** <https://forum.kx.com/t/re-bin-operator-questions/10160>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [July 21, 2015, 8:11pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160 "2015-07-21T20:11:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik\_friis1](https://avatars.discourse-cdn.com/v4/letter/e/ee59a6/32.png) [@erik\_friis1](https://forum.kx.com/u/erik_friis1)\
**Post date:** [July 21, 2015, 8:11pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/1 "2015-07-21T20:11:00Z")

</div>

OK after careful consideration these make sense to me and I guess there’s a rule that 4 \< 4 5 6 and 4 5 \< 4 5 6.

I would still like to know the rules regarding general lists containing items of mixed rank:

q)((0 0;0 0);(0 0;0 5)) bin ((0 0;0 5); 2 1)

1 1

It seems that according to these results that:

2 1 \> (0 0;0 5)

q)((0 0;0 0);(0 0;0 5)) bin ((0 0;0 5); 0 0 0; 0 0; 0; ((0 0;0 0);(0 0;0 0)))

1 1 1 -1 -1

And:

0 0 0 \> (0 0;0 5)

0 0 \> (0 0;0 5)

0 \< (0 0;0 0)

((0 0;0 0);(0 0;0 0)) \< (0 0;0 0)

Does not appear that when the shapes are the same, but the ranks are different that item-wise extension kicks in.

---

<div class="post-metadata">

**Author:** ![erik\_friis1](https://avatars.discourse-cdn.com/v4/letter/e/ee59a6/32.png) [@erik\_friis1](https://forum.kx.com/u/erik_friis1)\
**Post date:** [July 21, 2015, 10:01pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/2 "2015-07-21T22:01:00Z")

</div>

q)(::;((0 0;0 0);(0 0;0 0))) bin (::;0;0 0;(0 0;0 0);((0 0;0 0);(0 0;0 0)))

1 -1 1 1 1

Two more questions:

1. &nbsp;Why does the null item return a 1

2. &nbsp;Are atoms handled differently than lists? &nbsp;I would have expected 1 being returned from the 0 in the right operand.

---

<div class="post-metadata">

**Author:** ![erik\_friis1](https://avatars.discourse-cdn.com/v4/letter/e/ee59a6/32.png) [@erik\_friis1](https://forum.kx.com/u/erik_friis1)\
**Post date:** [July 21, 2015, 10:08pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/3 "2015-07-21T22:08:00Z")

</div>

q)5 2 3 1 4 bin 1 2 3 4 5

-1 1 3 4 4

Is the result undefined if the left operand is not sorted?

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 22, 2015, 7:09am UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/4 "2015-07-22T07:09:00Z")

</div>

These two cases seem straightforward?

> If there is no match the result is the count of the left argument.

On Wednesday, July 22, 2015 at 1:16:44 PM UTC+8, Erik Friis wrote:

> OK these two results I still don’t understand–I would have thought they’d be -1
> 
> q)(1 1;1 3;1 5)? 0 1
> 
> 3
> 
> q)(1 1;1 3;1 5)? 0 0
> 
> 3

---

<div class="post-metadata">

**Author:** ![erik\_friis1](https://avatars.discourse-cdn.com/v4/letter/e/ee59a6/32.png) [@erik\_friis1](https://forum.kx.com/u/erik_friis1)\
**Post date:** [July 22, 2015, 3:53pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/5 "2015-07-22T15:53:00Z")

</div>

Yes, sorry about that I’d typed the ? operator by mistake instead of the bin operator. &nbsp;I realized it late last night and deleted the original post as it makes perfect sense without the typo. &nbsp;My other questions, however, still stand. &nbsp;Thanks!

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 23, 2015, 8:46am UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/6 "2015-07-23T08:46:00Z")

</div>

I think the result, while incorrect, is understandable.

bin requires its left operand to be sorted. But (::;((0 0;0 0);(0 0;0 0))) is not sorted (try asc it). So when q performs binary search, it might actually start by comparing ((0 0;0 0);(0 0;0 0)) from the left operand with :: first, which will result in a value of 1 being returned.

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [July 23, 2015, 8:46am UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/7 "2015-07-23T08:46:00Z")

</div>

I think the result, while incorrect, is understandable.

bin requires its left operand to be sorted. But (::;((0 0;0 0);(0 0;0 0))) is not sorted (try asc it). So when q performs binary search, it might actually start by comparing ((0 0;0 0);(0 0;0 0)) from the left operand with :: first, which will result in a value of 1 being returned.

---

<div class="post-metadata">

**Author:** ![erik\_friis1](https://avatars.discourse-cdn.com/v4/letter/e/ee59a6/32.png) [@erik\_friis1](https://forum.kx.com/u/erik_friis1)\
**Post date:** [July 23, 2015, 3:37pm UTC](https://forum.kx.com/t/re-bin-operator-questions/10160/8 "2015-07-23T15:37:00Z")

</div>

Flying,

I do think you’re right regarding the fact that the left operand is not sorted being the culprit. &nbsp;So can we assume that if the left operand is not sorted that the result of bin is undefined?
