How to get the negate of decimal to binary?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the negate of decimal to binary?
# 1  
Old 12-11-2013
How to get the negate of decimal to binary?

Hi All,
New to this forum (and yes , a newbie in programming..Smilie)


I have a decimal to binary converter script done this way :

i=$1

bit0=$(( (i & 0x01) > 0 ))
bit1=$(( (i & 0x02) > 0 ))
bit2=$(( (i & 0x04) > 0 ))
bit3=$(( (i & 0x08) > 0 ))
bit4=$(( (i & 0x10) > 0 ))
bit5=$(( (i & 0x20) > 0 ))
bit6=$(( (i & 0x40) > 0 ))
bit7=$(( (i & 0x80) > 0 ))


Fisrt I would like to understand how the expression $(( (i & 0x01) > 0 )) works

Next, I need to obtain the same bits to its negate, so i.e. if for decimal 177

bit0=1
bit1=0
bit2=0
bit3=0
bit4=1
bit5=1
bit6=0
bit7=1


I need

bit8=0
bit9=1
bitA=1
bitB=1
bitC=0
bitD=0
bitE=1
bitF=0


is there a way to accomplish this inside the same script ??

Thank you very much.
# 2  
Old 12-11-2013
Are you sure it is a negate or an INVERT?

What you are showing looks like an INVERT...

So just subtract 177 from 255 and voila you have your decimal 78...

BTW is this homework?

Last edited by wisecracker; 12-11-2013 at 04:25 AM.. Reason: Corect my error...
# 3  
Old 12-11-2013
hmmmmm????

I never seen the thing under this aspect...

Effectively, let's take 98 :

01100010

do 255-98=157 that is

10011101

comparing the two:

01100010
10011101

Exactly the opposite cosidering bit to bit !!!!! Smilie

So I should keep the first part of the script to obtain the first conversion and add a second part to do 255-inputvalue and then again another conversion

like:


i=$1
n=(255-i)

bit0=$(( (i & 0x01) > 0 ))
bit1=$(( (i & 0x02) > 0 ))
bit2=$(( (i & 0x04) > 0 ))
bit3=$(( (i & 0x08) > 0 ))
bit4=$(( (i & 0x10) > 0 ))
bit5=$(( (i & 0x20) > 0 ))
bit6=$(( (i & 0x40) > 0 ))
bit7=$(( (i & 0x80) > 0 ))

bit8=$(( (n & 0x01) > 0 ))
bit9=$(( (n & 0x02) > 0 ))
bitA=$(( (n & 0x04) > 0 ))
bitB=$(( (n & 0x08) > 0 ))
bitC=$(( (n & 0x10) > 0 ))
bitD=$(( (n & 0x20) > 0 ))
bitE=$(( (n & 0x40) > 0 ))
bitF=$(( (n & 0x80) > 0 ))


??????



Is there perhaps a quicker negation of already producted single bit0-bit7 bits ??

I need it for a little project in BeagleBone GPIO, not quite homework...(homework should be done many years ago....when I was younger with less woody brain...Smilie )

Thank you

Last edited by digiteltlc; 12-11-2013 at 04:44 AM..
# 4  
Old 12-11-2013
Hi...

REMEBER! What I have shown you is for 8 bits...
IF you require 16 bits the subtract your number from 65535...
(Similarly for 32 bits, etc...)

Look at your problem logically...

"$1" is the first user argument of your shell script and it looks as though you will be entering your script as something like this:-
Code:
Your prompt> somescript 10101010<CR>

<CR> == carraiage return...

You have saved this argument inside your variable "i"

So "i" now contains a _string_ representation of your binary value...

Use something like:-
Code:
${i:position_in_string:1}

To change each bit...

This method keeps it all inside bash builtins...

I am at work ATM so replies will be sporadic from me so have a go yourself... ;o)
# 5  
Old 12-11-2013
The argument passed to script is a decimal number
Anyway the method you suggested me (255-i then reconversion) works 100%
Just tested on real circuit.

So i'll go that way

Thank you very much again
# 6  
Old 12-11-2013
Might I suggest using what is already available?
Code:
$ i=98
$ typeset -Z8 n=`echo "ibase=10;obase=2;255-$i"|bc`
$ echo $n
10011101
$


I hope that this is useful. I got the basis of this elsewhere on the board so I thought I should share it.


Robin
Liverpool/Blackburn
UK
# 7  
Old 12-11-2013
!!! very good as well.. !!!!

Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Negative decimal to binary

Is there a fast way to convert a negative decimal value into a signed binary number in bash script ? I've looked a lot on internet but I saw nothing... (For exemple : -1 become 11111111.) (9 Replies)
Discussion started by: Zedki
9 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. Programming

Binary to decimal for particular bits

Hello, I have script which work fine on particular data.file . The next feature I want to achieve is to get the decimal equivalent of data to data. The data looks like this : data(01000000000000000000110000000000) thank you.. #include <iostream> #include <fstream> #include... (4 Replies)
Discussion started by: emily
4 Replies

4. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

5. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

6. Shell Programming and Scripting

negate search help

Hi, I've tried a lot of negate codes in this forum, but they do not perform what I intended. Please help. inputfile: Paragraph1 contents: die1, die2, die3, pr_name1, pr_name2 pr_name3, pr_name4 Paragraph2 more contents: die1, die2, die3, pr_name1, pr_name2 pr_name3, pr_name4 ... (5 Replies)
Discussion started by: shamushamu
5 Replies

7. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

8. Programming

decimal to binary function error

I have the following simple code to return a binary number in a array format given an interger and the number of the bits for specifying the interger as binary number. #include <stdio.h> #include <stdlib.h> int main () { // int* get_binary_number(int* bit_array, int num, int... (8 Replies)
Discussion started by: return_user
8 Replies

9. Shell Programming and Scripting

unix script for converting a decimal to binary

Could anybody please help me in writing a script in unix for converting a decimal number to binary number. (3 Replies)
Discussion started by: softy
3 Replies
Login or Register to Ask a Question