bitwise and between two 32 bit binaries


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support bitwise and between two 32 bit binaries
# 1  
Old 11-28-2011
bitwise and between two 32 bit binaries

Hello All,
i have two 16 bit binaries that in two different variables, i want to perform a bitwise AND between the two and store the result in a different variable.

can anyone throw some light on doing this in a bourne shell...

eg var1= 1110101010101011

bit wise and with var2= 1111111111111110
----------------------
result = 1110101010101010

Can anyone throw some light on this one...

Thank you
Venu
# 2  
Old 11-28-2011
You cannot use bourne shell alone to do that. bash has bitwise operators as builtins.
You will have to resort to another interpreted language like perl, or a compiled language like C, if you cannot switch to bash at all.

c example:
Code:
// and.c usage:  ./and 34 0  not binary numbers
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
    if(argc==3)
    {
         int a=atoi(argv[1]);
         int b=atoi(argv[2]);
         printf("%d\n", a & b);
         return 0;
    }
    return 1;
}


Last edited by jim mcnamara; 11-28-2011 at 11:44 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 11-28-2011
Alright, so I was curious how to do this in bash, having never myself attempted it. It's a bit tricky.

First, you can do the operation directly using "Arithmetic Expansion", ie $(( ... ))
Code:
$ echo $(( 2#1110101010101011 & 2#1111111111111110 ))
60074

The "2#" tells bash to interpret the following string as a binary (base-2) number.
Without those, you get:
Code:
1108859625484546

ie, gibberish.

For variables, it's the same:
Code:
$ x=2#1110101010101011; y=2#1111111111111110
$ echo $((x&y))
60074

You can also tell bash to interpolate x and y as strings and then into binary numbers like this:
Code:
$ x=1110101010101011; y=1111111111111110
$ echo $(( 2#$x & 2#$y ))
60074

See how bash first substitutes $x as a string, then interprets the resulting token into a binary number?
This User Gave Thanks to otheus For This Post:
# 4  
Old 11-28-2011
can we compare the value in positions of the variables. For example break the bits and store in individual variables, and do the same with the second variable which is storing the other 16 bits.

ex 1010101010101010

break the value in to 16 variables so that

from right to left

var 0 = 0, var 1 = 1 var 2= 0 etc....till var 15

then

do the same with the second 16 bit binary

xvar 0 = 1 xvar 1 = 1 xvar 2 = 1


compare value

if var 0 = 0 and xvar 0 = 1 then result = 0..... store this in variable yvar 0

if var 1= 0 and xvar 1 = 1 then result = 0......store this in variable yvar 1

if var 2= 1 and xvar 2 = 0 then result = 0 ... store this in variable yvar 2
var [n] xvar[n] yvar[n]
so on till var 15

can anyone throw some light on doing this way.....

Thanks once again....
# 5  
Old 11-28-2011
Quote:
can anyone throw some light on doing this way.....

Thanks once again....
Maybe someone else will answer, but until then, why?
# 6  
Old 11-29-2011
You can do something like this to split the value into bits:

Code:
typeset -i value=2#1110101010101011
typeset -i bit
typeset -i i=0

while (( value ))
do
    bit[i++]=$(( value & 1 ))
    (( value >>= 1 ))
done

for i in ${!bit[@]}
do
    printf "bit %2d = %d\n" $i ${bit[i]}
done

If you run this script you get

Code:
bit  0 = 1
bit  1 = 1
bit  2 = 0
bit  3 = 1
bit  4 = 0
bit  5 = 1
bit  6 = 0
bit  7 = 1
bit  8 = 0
bit  9 = 1
bit 10 = 0
bit 11 = 1
bit 12 = 0
bit 13 = 1
bit 14 = 1
bit 15 = 1

This works with bash and ksh93.
# 7  
Old 11-29-2011
Id like to know why the original poster isnt able to use bash
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bitwise comparison of cols

Hello, I want to compute the bitwise number of matches in pairwise fashion for all columns. The problem is I have 18486955 rows and 750 columns. Please help with code, I believe this will take a lot of time, is there a way of tracking progress? Input Org1 Org2 Org3 A A T A ... (9 Replies)
Discussion started by: ritakadm
9 Replies

2. Shell Programming and Scripting

how to use bitwise or operator in /bin/sh

please any one can suggest me how to use bitesie || opearator to do this #initallize a=0 b=0 #condition if then a=0 else a=1 fi #bitwise or opeartion b = a || b Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: Palaniappan
3 Replies

3. FAQ Submission Queue

Analysis in bitwise XOR

The purpose of this article is revealing the unrevealed parts of the bitwise XOR. As we aware, the truth table for the XOR operator is : A B A^B 0 0 0 0 1 1 1 0 1 1 1 0 For example , 1^2 will be calculated as given below: First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Programming

bitwise and if

Hi Suppose we have these code lines: #define _IN_USE 0x001 /* set when process slot is in use */ #define _EXITING 0x002 /* set when exit is expected */ #define _REFRESHING 0x004 ... 1 main () { 2 3 unsigned r_flags =_REFRESHING; 4 5 if (r_flag &... (3 Replies)
Discussion started by: Puntino
3 Replies

5. Shell Programming and Scripting

Bitwise negation

I am taking an online course on Unix scripting. The topic is Unix arithmetic operators and the lesson is Logical and bitwise operations. It is not clear how much storage space Unix uses to represent integers that are typed. Bitwise negation caused me to question how many bits are used to... (3 Replies)
Discussion started by: dLloydm
3 Replies

6. UNIX for Advanced & Expert Users

bitwise operators

can anybody write a program to divide a number by another number using bitwise operators (9 Replies)
Discussion started by: areef4u
9 Replies

7. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

8. Programming

Bit-fields and Bitwise operators

Hi, Is it possible to use bitwise operators in bit fields? For example: typedef struct Mystruct { unsigned char A :1 ; unsigned char B :1 ; } Mystruct; and assume struct Mystruct STR_1S, STR_2S, tempSTRS = {0}; then the following line: tempSTRS = STR_1S & STR_2S; gives the... (3 Replies)
Discussion started by: amatsaka
3 Replies
Login or Register to Ask a Question