From: mallikarjunarao on
hi ,
how to identify odd 1's in a binary number my binary numbers is
110010001010101111110110
11010101010101111110110
11101101010101100011011
11111100110101010111101
From: mallikarjunarao on
On Aug 5, 4:44 pm, mallikarjunarao <malli....(a)gmail.com> wrote:
> hi ,
> how to identify odd 1's in a binary number my binary numbers is
> 110010001010101111110110
> 11010101010101111110110
> 11101101010101100011011
> 11111100110101010111101

for better understanding, identify 1 in the odd position of the binary
number
From: Andreas Leitgeb on
mallikarjunarao <malli.kv2(a)gmail.com> wrote:
> On Aug 5, 4:44 pm, mallikarjunarao <malli....(a)gmail.com> wrote:
>> hi ,
>> how to identify odd 1's in a binary number my binary numbers is
>> 110010001010101111110110
>> 11010101010101111110110
>> 11101101010101100011011
>> 11111100110101010111101
> for better understanding, identify 1 in the odd position of the binary
> number

{^(..)*.1} will match up to the first odd-positioned "1".
For further hits, remove an even number of digits from start including
the just found "1".

another approach:

set binNumber 110010001010101111110110
regsub -all {(..)} $binNumber {\1:} tmp
foreach h [regexp -all -indices -inline {1:} $tmp] {
set realIndex [expr {[lindex $h 0]/3*2+1}]
puts "pos $realIndex: [string index $binNumber $realIndex]"
}

If I mixed up odd and even (that is, if positions are meant as 1-based),
then change the regexp (in foreach) to {1.:} and remove the "+1" in the
expr.

From: Uwe Klein on
Andreas Leitgeb wrote:
> mallikarjunarao <malli.kv2(a)gmail.com> wrote:
>
>>On Aug 5, 4:44 pm, mallikarjunarao <malli....(a)gmail.com> wrote:
>>
>>>hi ,
>>>how to identify odd 1's in a binary number my binary numbers is
>>>110010001010101111110110
>>>11010101010101111110110
>>>11101101010101100011011
>>>11111100110101010111101
>>
>>for better understanding, identify 1 in the odd position of the binary
>>number
>
>
> {^(..)*.1} will match up to the first odd-positioned "1".
> For further hits, remove an even number of digits from start including
> the just found "1".
>
> another approach:
>
> set binNumber 110010001010101111110110
> regsub -all {(..)} $binNumber {\1:} tmp
> foreach h [regexp -all -indices -inline {1:} $tmp] {
> set realIndex [expr {[lindex $h 0]/3*2+1}]
> puts "pos $realIndex: [string index $binNumber $realIndex]"
> }
>
> If I mixed up odd and even (that is, if positions are meant as 1-based),
> then change the regexp (in foreach) to {1.:} and remove the "+1" in the
> expr.
>
if {[string length $binNumber] % 2} {
....
}

uwe
From: Bezoar on
On Aug 5, 6:44 am, mallikarjunarao <malli....(a)gmail.com> wrote:
> hi ,
> how to identify odd 1's in a binary number my binary numbers is
> 110010001010101111110110
> 11010101010101111110110
> 11101101010101100011011
> 11111100110101010111101

Are you saying you want to identify the binary numbers where there is
a 1 in and odd position(e.g 001, 1 is in position 3 (if count starts
at 1 ) ), the resuting number is odd, the count of ones showing up in
the binary number is odd?
for the second just check to see if last digit is 1

proc isOddNum { num } {
return [expr ([string index $num end ] == "1") ? 1 : 0 ]
}

for the latter :

set num 11111100110101010111101
proc isCountOfOnesOdd { num } {
# compress 0's then apply modulus 2 to resulting string
return [expr {[string length [string match {"0" ""} $num ] ] %
2 }];
}